get_post_type()返回错误的值 - wp_reset_query()无法重置全局$ post

时间:2016-07-12 00:04:22

标签: wordpress custom-post-type

我在我的(404)页面的主标题部分中发起new WPQuery

  • 在我这样做之前,get_post_type()函数不返回任何结果 - 正如它应该做的那样。
  • 但在new WPQuery之后,尽管我使用wp_reset_query()get_post_type()函数会返回上一个查询中最后一个帖子的帖子类型,而不是当前帖子的帖子类型(无)。

    echo get_post_type();
    // returns nothing
    $args = array ( 'post_type' => 'guide', 'post_parent' => 0, 'order' => 'ASC' );
    $custom_query = new WP_Query($args);
    
    if( $custom_query2->have_posts() ) {
        while ($custom_query2->have_posts()) : $custom_query2->the_post();
    
            echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
    
        endwhile;
    }
    wp_reset_query();
    
    echo get_post_type();
    // despite wp_reset_query() it returns the post type of last post in the previous query
    

如何才能正确重置查询,以便从get_post_type()获得与查询之前相同的结果(无)?

0 个答案:

没有答案