WP_Query头痛 [英] WP_Query headache
问题描述
我正在尝试使用WP_Query输出带有标签的最新3篇帖子.我在stackoverflow上问过这个问题,并从一个好心人那里得到了很好的建议.这就是我现在拥有的:
I am trying to use WP_Query to output the 3 latest posts with the tag featured. I asked about it here on stackoverflow and got at good tip from a kind person. This is what I have now:
<?php
$home_featured = new WP_Query(array(
'tag' => 'featured',
'posts_per_page' => 3,
));
?>
<?php if ($home_featured->have_posts()): while ($home_featured->have_posts()) : $home_featured->the_post(); ?>
<p>Got some</p>
<?php endwhile; ?>
<?php else: ?>
<p>None found</p>
<?php endif; ?>
现在,我有3篇带有标签的帖子.由于Wordpress在此处使用while循环,因此每个帖子最多应执行一次迭代,最多3次,输出
Got some
Now, I have 3 posts that have the tag featured. Since Wordpress uses a while loop here, it should do one iteration per post up to 3 times, outputting a
Got some
有一些 得到一些 有一些
Got some Got some Got some
但是它只输出一次,就像这样:
But it only outputs it one time, like this:
有一些
怎么了?
推荐答案
首先,将<p>Got some</p>
替换为<?php the_title(); ?>
.至少您会看到正在显示哪些帖子.
First thing, replace <p>Got some</p>
by <?php the_title(); ?>
. At least you'll see what posts are being shown.
您也可以在开始wordpress循环之前执行print_r( $home_featured );
.您将获得传递给WP_Query
的所有参数,为查询生成的SQL以及已返回的所有帖子.
You could also do a print_r( $home_featured );
just before starting the wordpress loop. You'll get all the parameters passed to WP_Query
, the SQL generated for the query and all the posts that have been returned.
显示$home_featured->found_posts
还将显示与当前查询参数匹配的帖子总数,从而为您提供帮助.
Echoing $home_featured->found_posts
will also help you by displaying the total number of posts found matching the current query parameters.
这篇关于WP_Query头痛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!