我试图在Wordpress的索引页面上运行两个独立的循环:
没有类别5的帖子的人
和
一个只有类别5的帖子
我总是得到接近正确的结果,但第一个循环显示了我在第二个循环中想要的东西(这让我感到困惑),以及一堆其他异常情况。
代码如下:
循环1:
<?php $blogPreview = new WP_Query('cat=-5'); ?>
<?php if ( $blogPreview->have_posts() ) : ?>
<?php while ( $blogPreview->have_posts() ) : $blogPreview->the_post(); ?>
<?php the_post(); ?>
<?php get_template_part( 'content-index', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
Sorry, but there are currently no posts in the blog!
<?php endif; ?>
<?php wp_reset_query(); ?>循环2(在我的标记中略低于循环1):
<?php $testimonials = new WP_Query('cat=5'); ?>
<?php if ( $testimonials->have_posts() ) : ?>
<?php while ( $testimonials->have_posts() ) : $testimonials->the_post(); ?>
<?php the_post(); ?>
<?php get_template_part( 'content-index-testimonials', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
Sorry, but there are currently no posts in the blog!
<?php endif; ?>
<?php wp_reset_query(); ?>有什么想法吗?提前感谢
好的,所以通过从循环中删除"the_post()“修复了这个问题。
现在我终生不能排除这个类别,我已经尝试了以下所有方法:
<?php $blogPreview = new WP_Query('cat=-5'); ?>
<?php $blogPreview = new WP_Query(array('category__not_in'=>5)); ?>
<?php $blogPreview = new WP_Query('category__not_in'=>array(5)); ?>都没有做到这一点。
发布于 2012-02-29 20:25:37
不确定,但我怀疑这可能与
<?php the_post(); ?>在你已经为WP_Query对象(如$testimonials->the_post();)调用了它之后。我认为该调用将使用全局$wp_query值,并覆盖上一次调用中设置的值。试着删除它。
发布于 2012-02-29 20:49:03
在这两个循环中,删除“the_post();”,因为您已经调用了$testimonials>the_post();
<?php $testimonials = new WP_Query('cat=5'); ?>
<?php if ( $testimonials->have_posts() ) : ?>
<?php while ( $testimonials->have_posts() ) : $testimonials->the_post(); ?>
<?php // i.e. You can call here, the_title(); ?>
<?php get_template_part( 'content-index-testimonials', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry, but there are currently no posts in the blog!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>https://stackoverflow.com/questions/9490635
复制相似问题