问题描述
我正在尝试在同一页面上查询多种不同类型的帖子。当我第二次尝试查询 (Essays) 时,没有显示任何内容,这意味着我的 if ($arr_posts->have_posts())
评估为假。第一个查询和第三个和第四个查询工作正常。第二个查询一直有效,直到我在它之前添加了这个采访查询。甚至当我评论它时,它仍然停止显示。我错过了什么? mwp_interview
是自定义帖子类型。
<!--Latest Interview-->
<?PHP
$args = array(
'posts_per_page' => 1,'post_status' => 'publish','post_type' => 'mwp_interview',);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
disPLAY INTERVIEW POST
</div>
<?PHP
endif; ?>
<!--Latest Essay-->
<?PHP
wp_reset_query();
wp_reset_postdata();
$args = array(
'post_type' => 'post','category_name' => 'Essays in discipleship','posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
disPLAY ESSAY POST
</div>
<?PHP
endif; ?>
<!--Latest Special Series-->
<?PHP
$ss_args = array(
'post_type' => 'post','category_name' => 'Special Post','posts_per_page' => 1,);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );
if ( $ss_arr_posts->have_posts() ) :
$ss_arr_posts->the_post();
?>
<div>
disPLAY SPECIAL SERIES POST
</div>
<?PHP
endif;
?>
<!--Latest podcast-->
<?PHP
$args = array(
'post_type' => 'post','category_name' => 'podcast',);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
disPLAY podcast
</div>
<?PHP
endif;
?>
解决方法
根据Wordpress documentation WP_Query Category Parameters。
category_name
(string) – 使用类别 slug。
参数名称 category_name
实际上具有误导性,category_name
指的是 类别 SLUG,而不是实际类别名称。
<?php
$args = [
'posts_per_page' => 1,'post_status' => 'publish','post_type' => 'mwp_interview',];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo "<hr>";
the_title( '<h1>','</h1>' );
endwhile;
else:
echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post','category_name' => 'essays-in-discipleship',// ... must be set set to "Essays In Discipleship" category slug
'posts_per_page' => 1,];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>','</h1>' );
endwhile;
else:
echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post','category_name' => 'special-post',// ... must be set to "Special Post" category slug
'posts_per_page' => 1,'</h1>' );
endwhile;
else:
echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>