Wordpress - 类别未显示所有帖子
<?php
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
$cats = get_the_category(); ?>
<?php if ($cats[0]->cat_name === 'Coaching') { ?>
<div class="callout horizontal">
<?php the_post_thumbnail(); ?>
<div class="content">
<h5><?php the_title(); ?></h5>
<?php the_content(); ?>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
The above code is meant to only show categories that are from the category Coaching. However this happens for two of my category posts but not for the other three that are labeled under Category.
It is worth noting that I have set my reading posts in the wordpress settings to 9999 just incase this was a pagination problem.
Can anyone see what the error might be.
I would like to also mention that this logic was working not so long ago before I added a lot more posts.
Cheers
************** UPDATE ***************
To add to the mystery I have noticed that this is happening on another part of the site. Thats isn't related to the posts. But pulling out from Advanced Custom Fields.
&lt;?php
$ wpb_all_query = new WP_Query(array('post_type'=&gt;' post','post_status'=&gt;'publish','posts_per_page'=&gt; -1));
if if($ wpb_all_query-&gt; have_posts()):?&gt;
&lt;? php while($ wpb_all_query-&gt; have_posts()):$ wpb_all_query-&gt; the_post();
$ cats = get_the_category(); ?&gt;
&lt;?php if($ cats [0] - &gt; cat_name ==='Coaching'){?&gt;
&lt; div class =“callout horizontal”&gt;
&lt; ?php the_post_thumbnail(); ?&gt;
&lt; div class =“content”&gt;
&lt; h5&gt;&lt;?php the_title(); ?&gt;&lt; / h5&gt;
&lt;?php the_content(); ?&gt;
&lt; / div&gt;
&lt; / div&gt;
&lt;?php}?&gt;
&lt;?php endwhile; ?&gt;
&lt;?php endif; ?&gt;
code> pre>
上述代码仅用于显示Coaching类别中的类别。 但是,对于我的两个类别帖子会发生这种情况,但对于在“类别”下标记的其他三个帖子则不会 p>
值得注意的是,我已将wordpress设置中的阅读帖子设置为9999,这只是一个分页问题。 p>
任何人都可以看到错误可能是什么。 p>
我还想提一下,在我添加更多帖子之前,这个逻辑不是很久以前的工作。 p>
干杯 p>
**************更新************ *** p>
为了增加神秘感,我注意到这发生在网站的另一部分。 这与帖子无关。 但退出高级自定义字段。 p>
div>
EDIT:
I've seen now that your post query will return all posts, and you're checking for category inside the loop, which is kinda redundant. Modify your query like this:
$wpb_all_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'coaching', //slug, not name!!
'posts_per_page' => 9999
)
);
This will pull only posts from that category. :)