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() . '… <br>
<a href="' . get_permalink() . '">More ›</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 . '… <a href="' . get_permalink() . '">More ›</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="#">›</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!