Home › Forums › General Issues › Category list not working inside of while loop
I’ve got a rather simple section that is made up of a single repeater (category_articles) that contains two fields, a headline (headline) and a taxonomy selector (post_category, returns Term ID).
Here’s the code block I’m using:
<section class="multi_column_articles">
<div class="container">
<div class="row">
<?php if( have_rows('category_articles') ):
while( have_rows('category_articles') ): the_row();
$headline = get_sub_field( 'headline' ); //text headline
$post_category = get_sub_field( 'category' ); //category ID
?>
<div class="col">
<h4><?php echo $headline; ?></h4>
<?php
$args = array( 'category' => $post_category, 'post_type' => 'post' );
$postslist = get_posts( $args ); //get posts in specified category
foreach ($postslist as $post) : setup_postdata($post); ?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endforeach; ?>
</div>
<?php endwhile;
endif;?>
</div>
</div>
</section>
The problem I’m running into is the while loop only appears to go through one time and then quits. I’ve got three categories set up and have posts in each category. On the page I want it to display each category list in a column with the ability to add/remove columns later on.
I’ve tested the while loop without the post list in it and it works fine, I’ve also tested the post list by itself (with multiple categories at the same time) and it also works fine.
Has anyone else run into an issue like this?
I figured out that the post data was causing an issue adding in wp_reset_postdata() before the final endwhile solved the issue.
<?php endforeach; ?>
</div>
<?php wp_reset_postdata();
endwhile;
endif;?>
</div>
You must be logged in to reply to this topic.
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.