我已经设置了一个自定义帖子类型,其中包括以下结构:
- 主帖子
- 儿童职位
- 同胞职位
- 儿童职位
我有两个主要的帖子,称为" vrouwen" en"曼南"当我访问这些主要帖子之一时,我只想显示兄弟姐妹。
我坚持实现这一目标。
,但随后也显示了"孩子"。我需要以某种方式更深入一个水平。
所有帮助都非常感谢!
我尝试了下面的代码。
$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();
,但随后也显示了"孩子"。我需要以某种方式更深入一个水平。
首先, 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();
}