0

For some reason, when viewing a category, the posts are repeated - basically the whole loop repeats as many times as there are posts for that category.

E.G.:
If category_a has 3 posts, the page will display:

post1 - Featured
post2
post3

post1 - Featured
post2
post3

post1 - Featured
post2
post3

If category_a has 4 posts it will display:

post1 - Featured
post2
post3
post4

post1 - Featured
post2
post3
post4

post1 - Featured
post2
post3
post4

post1 - Featured
post2
post3
post4

I'm using the same post-template (content-feed.php) for the main blog page and for category pages. It works fine on the main blog page.

I'm not sure if it matters or has anything to do with this issue, but may be noteworthy: that in the loop I'm first pulling a "featured" post - and then the rest of the posts ("not-featured") below. Again - this works just fine on the main blog page, this issue is only on category pages.

Here is the entire post-template (content-feed.php):

<div class="row">

<?php while (have_posts()) : the_post(); ?>

        <?php               
            $args = array(
               'posts_per_page' => 1,
               'post_type' => 'post',
               'order' => 'DESC', 
               'category__in' => ($cat),
               'meta_query' => array( 
                  array(
                     'key' => 'is_featured', 
                     'value' => '1', // only display the latest post marked featured, if there is one
                  )
               )
            );

            $first_query = new WP_Query( $args );
            if ( $first_query->have_posts() ):

            echo '<div class="widget-box featured beige right">';

               while( $first_query->have_posts() ) : $first_query->the_post();


               $attachment_id = get_field('image');
               $size = 'customfeatblg'; // (thumbnail, medium, large, full or custom size)
               $image = wp_get_attachment_image_src( $attachment_id, $size ); 

                       if( get_field('image') ):
                           $thisimage = '<a href="' . get_permalink() . '"><img src="' . $image[0] . '" alt="' . get_the_title() .'" width="290" height="92" /></a>';  
                           $divclass = 'summary';
                       else:
                           $divclass = 'fullsummary';
                       endif; 

                       if( is_category() ):
                            $category = strip_tags( single_term_title('',false) );
                          echo '<div class="title main fs-45">' . $category .'<span></span></div>';
                       else:
                            $category = get_the_category();
                          echo '<div class="title main fs-45">' . $category[0]->cat_name .'<span></span></div>';
                       endif;
        ?>

        <?php
            echo '

                ' . get_template_part('templates/social-shareing') .'

                    <div class="the-content">

                        ' . $thisimage .'
                        <div class="' . $divclass . '">
                            <h2 class="purple"><a href="' . get_permalink() . '">' . get_the_title() .'</a></h2>
                            <p class="date">' . get_the_date() .'</p>
                            <p>' . get_the_excerpt() . '&hellip; <br>
                            <a href="' . get_permalink() . '">More &rsaquo;</a></p>
                        </div>

                    </div>


                ';

           endwhile;
        ?>

        <?php
            echo ' <div class="bottom-cap"></div>
                </div><!-- [END] widget-box -->';
            endif;
            wp_reset_postdata(); // to reset the loop               
        ?>


    <div class="feed med">
    <?php               
        $args = array(
           'posts_per_page' => 10,
           'post_type' => 'post',
           'category__in' => ($cat),
           'meta_query' => array( 
              array(
                 'key' => 'is_featured', 
                 'value' => '1',
                 'compare' => '!=', // only display posts not marked as featured - to prevent repeat from above
              )
           )
        );

        $second_query = new WP_Query( $args );
        if ( $second_query->have_posts() ):
           while( $second_query->have_posts() ) : $second_query->the_post();

           $titlechars = 45; // Character Limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo '<article>';
    ?>

    <?php
        if( get_field('image') ):

            $attachment_id = get_field('image');
            $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo '<a href="' . get_permalink() . '"><img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" /></a>';
    ?>

    <?php else : ?>

        <?php echo '<a href="' . get_permalink() . '"><img src="'. get_template_directory_uri() .'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" /></a>' ?>

    <?php endif; ?>

    <?php
        echo '
            <h3 class="purple"><a href="' . get_permalink() . '">' . $modtitle .'</a></h3>
            <p class="date">' . get_the_date() .'</p>
            <p>' . $modcontent . '&hellip; <a href="' . get_permalink() . '">More &rsaquo;</a></p>

            </article>';
    ?>

    <?php                              
        endwhile;
        endif;
        wp_reset_postdata(); // to reset the loop
    ?>
    </div><!-- [END] feed-->

<?php/*  <div class="pagination"><a href="#">1</a><a href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">&rsaquo;</a></div>  */?>
<?php
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
?>

</div><!-- [END] row -->
<?php endwhile; ?>

And here is my category.php template:

<?php get_template_part('templates/nav', 'category'); ?> 
<?php get_template_part('templates/content', 'feed'); ?>
<?php if (hchw_sidebar()) : ?>
    <?php get_template_part('templates/sidebar', 'right'); ?>
<?php endif; ?>

I tried adding/moving <?php wp_reset_query(); ?> after <?php endwhile; ?> in content-feed.php but that didn't change anything.

Any ideas on how to fix this or prevent it from happening?

Thanks in advance for any help!

2 Answers 2

2

You have three loops, two nested inside the main loop. The main loop display the posts e.g. in category_a. What does the second nested loop display? It seems this loops also display the posts from the samne category as the main loop. So try to remove the second nested loop.

1
  • hi Ralf912, thanks for responding and your help! The first nested loop displays the featured post. The second nested loop displays all other posts that are not marked as featured. All posts are displayed as many number of times as there are posts in that category. If I remove either of the nested loops, I still get the same result as far as the loop repeating as many times as there are posts in the category. Dec 25, 2012 at 2:26
2

Ahhhh, thanks to Ralf912's help troubleshooting, I was able to figure out the solution. I needed to remove the "parent" loop - so replacing <?php while (have_posts()) : the_post(); ?> with <?php if (have_posts()) : ?> and replacing the closing <?php endwhile; ?> with <?php endif; ?> fixed it.

There was no need to nest the featured and non-featured loops within a parent loop.

Thanks again Ralf912!

1
  • that's what stackexchange are made for. if you get stuck and stumble, someone will help you back on your feets. but no one will run for you ;)
    – Ralf912
    Dec 25, 2012 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.