从自定义帖子类型获取同级

时间:2019-05-07 09:18:03

标签: php wordpress

我使用以下结构设置了自定义帖子类型:

  • 主要职位
    • 儿童张贴
      • 同级帖子

我有两个主要帖子,分别是“ Vrouwen”和“ Mannen” 当我访问这些主要职位之一时,我只想显示同级。

我坚持实现这一目标。

但是随后还会显示“孩子”。 我需要以某种方式更深一层。

感谢所有帮助!

我尝试了下面的代码。

$mysibling        = $post->post_parent;
$mychild          = $post->ID;
$mychildmysibling = array( $mychild, $mysibling );

$args = array(
    'post_parent'    => $mychildmysibling,
    'post__not_in' => array( $post->ID ),
    'posts_per_page' => -1,
    'post_type'       => 'collectie'
);

$parent = new WP_Query( $args );
while ( $parent->have_posts() ) : $parent->the_post();

但是随后还会显示“孩子”。我需要以某种方式更深一层。

1 个答案:

答案 0 :(得分:0)

首先post_parent需要一个数字,但是您设置了一个数组。其次,您基本上需要使其仅适用于主页吗?因此查询应如下所示:

// find children for the main post
$children = get_children( array('post_parent' => $post->ID));

// check if the post has any children
if ( ! empty($children) ) {
   // get all posts where the parent is set as children to main post
   $args = array(
        'post_parent__in' => array_keys($children),
        'posts_per_page' => -1,
        'post_type' => 'collectie'
    );

   $siblings = new WP_Query( $args );

   if ( $siblings->have_posts() ) {
       while ( $siblings->have_posts() ) {
           $siblings->the_post();

           echo get_the_title();
       }
   }

   wp_reset_postdata();
}
相关问题