出于某种原因,在查看类别时,帖子会重复-基本上整个循环重复的次数与该类别的帖子重复的次数相同。
E.G.:
如果category_a
有3篇文章,页面将显示:
post1-特色
post2
post3
post1-特色
post2
post3
post1-特色
post2
post3
如果category_a
将显示4个帖子:
post1-特色
post2
post3
post4
post1-特色
post2
post3
post4
post1-特色
post2
post3
post4
post1-特色
post2
post3
post4
我使用相同的帖子模板(content-feed.php
) 对于主博客页面和类别页面。它在主博客页面上运行良好。
我不确定这是否重要或与此问题有关,但可能值得注意的是:在循环中,我首先拉一个“特色”帖子,然后拉下面的其他帖子(“非特色”)。同样,这在主博客页面上效果很好,这个问题只在分类页面上出现
这是整个帖子模板(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; ?>
这是我的
category.php
模板:
<?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; ?>
我尝试添加/移动
<?php wp_reset_query(); ?>
之后
<?php endwhile; ?>
在里面
content-feed.php
但这并没有改变任何事情。
有没有关于如何解决或防止这种情况发生的想法?
提前感谢您的帮助!