【问题标题】:Removing posts with a parent from the loop从循环中删除带有父级的帖子
【发布时间】:2013-12-11 20:43:14
【问题描述】:

我正在尝试从我网站上的模板页面中删除带有 post_parent 的帖子。我能够通过以下方式阻止它们在循环中显示:

if (have_posts()) : while (have_posts()) : the_post(); 
    if (($post->post_parent) ) {
        continue;
    } else {
        //code to display post
    }

问题是分页仍然显示在页面上,因为查询中的帖子仍然比我每页的帖子多。这会导致分页中出现许多空白页。

我尝试过以下几种变化:

function exclude_children( $query ) {
          $query->set('post_parent', 0 );
    }
    add_action( 'pre_get_posts', 'exclude_children' );

还有:

$where .= ' AND post_parent = 0';

在我的循环模板上的 query_posts() 命令无效之前。

【问题讨论】:

    标签: wordpress posts


    【解决方案1】:

    试试this

      $query = new WP_Query( 'post_parent=0' ); //Display only top-level pages, exclude all child pages:
    

    【讨论】:

    • 所以我正在使用的模板页面已经在使用查询(从特定类别中获取所有自定义帖子“产品”)。我尝试使用 array_merge,但它似乎没有做任何事情:array_merge( $wp_query->query, array( 'post_parent' => 0 ) );
    • @OrthoClassic-Chris,您不应该合并数组,这对您不利(esspessilay 以您现在的方式)。您应该将 'post_parent=0' 插入到您现有的查询参数中。
    猜你喜欢
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多