I'm looking for a way to connect the Elementor Posts Widget to display an additional H2 tag under the post title of each post.
I will then get this H2 value from the single post ACF field.
From other things I've read, there are ways to get the entire HTML of the output as a string, but this requires a lot of string replacement, so is not very future-proof. For example: Hook into elementor widget? https://developers.elementor.com/docs/hooks/render-widget-content/
If I use code like this, is there a way to hang it behind the post title? Or is string replacement the best way to solve this problem?
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 the help. Thanks
P粉7060387412023-12-14 09:25:51
If you dig into the Elementor Pro source code, you'll find a great tip: Dynamic Label -> ACF Module
get_queried_object()
Or try this: Dynamic Label -> 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')
instead of $post_id