I’ve got a page template that uses an ACF repeater to display a grid of news articles. These are not blog posts, but a way to display external press that the client occasionally receives.
The repeater field is called “news_article.” It contains the following sub-fields:
- “article_title” (text field)
- “source_link_text” (text field)
- “source_link” (URL field)
- “article_date” (date picker field)
- “article_pull_quote” (text area field)
- “article_description” (text area field)
- “article_link” (URL field)
- “homepage_featured_article” (true/false field)
On the homepage of my site, I have a section where I’d like to display the “article_pull_quote” subfield based on whether or not the “homepage_featured_article” true/false field is clicked.
I tried to implement this in my page template like this:
<div class="feature-article">
<h5>In the news</h5>
<?php if( get_field('homepage_featured_article') ): ?>
<p><?php
if( have_rows('news_article') ):
while( have_rows('news_article') ) : the_row();
$value = get_sub_field('article_pull_quote');
endwhile;
endif;
?></p>
<?php endif; ?>
<a href="<?php echo get_site_url(); ?>/in-the-news/">See all news</a>
</div>
So far, it doesn’t work. I’m wondering if it has to do with the way I’ve nested my repeater?
Any advice would be much appreciated!
When getting fields from another post you need to supply the post ID of the post you want to get them from. In the case of a repeater, this only needs to be done for the top level field
if (have_rows('repeater_field', $post_id)) {
while (have_rows('repeater_field', $post_id)) {
the_row();
get_sub_field('sub_field_name');
}
}
More information here https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
This reply has been marked as private.