Home Forums General Issues Category list not working inside of while loop


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>
                    $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; ?>
                <?php endwhile; 

    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; ?>
      <?php wp_reset_postdata();
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.