微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wp_reset_query 未重置

如何解决wp_reset_query 未重置

我正在尝试在同一页面查询多种不同类型的帖子。当我第二次尝试查询 (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(); ?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。