<?php
$args = array(
\'post_type\'=>\'weather_today\',
\'orderby\'=>\'ID\',
\'order\'=>\'ASC\',
\'posts_per_page\'=>1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ( have_posts() ) : the_post();
the_content();
endwhile;
}
wp_reset_postdata();
?>
输出帖子内容(
the_content()) 那不是
weather_today 类型为什么会这样?我检查了我的SQL,在
wp_posts 我只有一个职位
post_type = "weather_today" 而且它不是被输出的那个。此查询位于我的标题中。。。我相信以上任何其他自定义查询。此外,似乎其他参数都受到尊重,我得到的职位只是
1 最后一篇文章是
ID.
那么为什么post_type, 是否忽略此查询中最重要的参数?
SO网友:Krzysiek Dróżdż
代码的问题很简单。。。您使用您的自定义$query 在if语句中,但使用全局$wp_query 在while循环中。。。
<?php
$args = array(
\'post_type\'=>\'weather_today\',
\'orderby\'=>\'ID\',
\'order\'=>\'ASC\',
\'posts_per_page\'=>1
);
$query = new WP_Query($args); // <- here you create custom $query
if ($query->have_posts()) { // <- here you check if it has any posts
while ( have_posts() ) : the_post(); // <- here you don\'t use it
// it should be: while ( $query->have_posts() ) : $query->the_post();
the_content();
endwhile;
}
wp_reset_postdata();
?>
另一个建议-不要使用
{ 和
: 同一上下文中的符号-不利于可读性;)