0

I'm looking for a way to hook into an Elementor Posts Widget to display an extra H2 tag under the posts title for each posts.

I would then get this H2 value from from the single posts ACF field.

From what I am reading else where there are ways to get the whole HTML of the output as string, but that requires a lot of string replace and so not very future proof. Eg: Hook into elementor widget? https://developers.elementor.com/docs/hooks/render-widget-content/

If I am using a code like this is there a way to hook this after the Post title? or is string replace the best way to approach this?

function change_heading_widget_content( $widget_content, $widget ) {

if ( 'posts' === $widget->get_name() ) {
    $settings = $widget->get_settings();
    $post_id = "Somehow get the post id (maybe look for in the $widget_content string per post?)";

    if ( ! empty( $settings['link']['is_external'] ) ) {
        $widget_content .= '<h2>'. get_field("extra_heading", $post_id) .'<h2>';
    }
}

return $widget_content;

}
add_filter( 'elementor/widget/render_content', 'change_heading_widget_content', 10, 2 );

I appreciate all and any help. Thanks

GediTheJedi
  • 21
  • 1
  • 7

1 Answers1

1

If you dig into Elementor Pro source code you'll find a good hint: Dynamic Tags -> ACF Module

get_queried_object()

Or try this: Dynamic Tags -> ACF Module Rendering

function get_queried_object_meta( $meta_key ) {
        $value = '';
        if ( is_singular() ) {
            $value = get_post_meta( get_the_ID(), $meta_key, true );
        } elseif ( is_tax() || is_category() || is_tag() ) {
            $value = get_term_meta( get_queried_object_id(), $meta_key, true );
        }

        return $value;
    }

Or just use get_field('my-field') without $post_id

Samuel
  • 11
  • 2
  • Thanks a lot! So from what I understand above you are grabbing the specific ACF field from the post. How does one then implement this to run in the Elementor Loop to render the posts? thanks again – GediTheJedi Aug 09 '22 at 13:47