WP_Query headache

久未见 提交于 2020-01-25 11:17:49

问题


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; ?>

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

. This should result in something like this on the screen:

Got some Got some Got some

But it only outputs it one time, like this:

Got some

What is wrong?


回答1:


First thing, replace <p>Got some</p> by <?php the_title(); ?>. At least you'll see what posts are being shown.

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.

Echoing $home_featured->found_posts will also help you by displaying the total number of posts found matching the current query parameters.



来源:https://stackoverflow.com/questions/15682329/wp-query-headache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!