繁体   English   中英

wp_reset_query 未重置

[英]wp_reset_query not resetting

我正在尝试在同一页面上查询几种不同类型的帖子。 当我第二次尝试查询(论文)时,什么都没有出现,这意味着我的if ($arr_posts->have_posts())评估为假。 第一个查询以及第三个和第四个查询工作正常。 第二个查询一直有效,直到我在它之前添加了这个 Interview 查询。 即使我评论了它,它仍然停止出现。 我错过了什么? 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',
    'post_status' => 'publish',
    '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',
    'post_status' => 'publish',
    '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',
    'post_status' => 'publish',
    'category_name' => 'Podcast',
    'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    $arr_posts->the_post();
    ?>
    <div>
        DISPLAY PODCAST
    </div>
    <?php
endif;
?>

根据Wordpress 文档 WP_Query 类别参数

category_name (string) – 使用类别 slug。

参数的名称category_name实际上具有误导性, category_name指的是类别 SLUG,而不是实际的类别 NAME

<?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',
  'post_status' => 'publish',
  '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',
  'post_status' => 'publish',
  'category_name' => 'special-post', // ... must be set to "Special Post" 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 "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM