Home › Forums › Add-ons › Flexible Content Field › Pass field from Flexible Content into a widget › Reply To: Pass field from Flexible Content into a widget
Hi Elliot, thank you for getting back to me.
I’m using the Relationship field with Flexible Content to create a list of selected posts which is called Featured Posts:
<?php
while( has_sub_field( 'page_builder' ) ):
if( get_row_layout() == 'featured_posts' ):
get_template_part ( 'builder/featured', 'posts' );
endif;
endwhile;
?>
The loop inside featured-posts.php looks like this:
<?php
$featured_posts = get_sub_field( 'featured_post_add' );
if( $featured_posts ):
foreach( $featured_posts as $post ): ?>
<article>
...
</article>
<?php
endforeach;
wp_reset_postdata();
endif;
?>
Everything works great.
Now what i’m trying to do, is to output the exact same posts, added in this Relationship field, in a widget. I have created a widget file featured-posts-widget.php which i drag into the sideabr. The thing is that the posts are not showing up and the widget is blank, besides the title, no matter what type of loop or a query i use. The code among others i tried is:
<?php
global $post;
$featured_posts_sidebar = get_field( 'featured_post_add' );
if( $featured_posts_sidebar ):
foreach( $featured_posts_sidebar as $post ): ?>
<article>
...
</article>
<?php
endforeach;
wp_reset_postdata();
endif;
?>
In the widget i use get_field and not get_sub_field. Maybe using WP_Query and not the foreach loop would be a solution, but what parameters do i have to use?
Can you point me to the right direction?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.