例如,我有这个菜单,并将其添加到页面的"关于"one_answers"空缺"子页面中,并将它们添加到页面设置中
Home
About
-- History
-- Photo's
Vacancies
-- Front-end developer
-- Full-stack developer
Contact
当我在子页面上时,如何获取当前主页面?例如,我在主页上关于,比我想要所有子页面的列表(在这个例子中是历史和照片(
页面空缺也是如此,当我在主页上时,我想获得所有子页面的列表。
我在下面试过了,但后来我得到了cpt"page"的所有页面。不是我需要的孩子。有人能帮忙吗?:(
<?php
global $post;
$args = array(
'child_of' => $post->ID,
'post_type' => 'page',
'posts_per_page' => -1,
'order' => 'ASC',
'depth' => 1,
'orderby' => 'date',
);
$parent = new WP_Query($args); ?>
<?php if ($parent->have_posts()) : ?>
<div class="submenu-pages">
<ul>
<?php while ($parent->have_posts()) : $parent->the_post(); ?>
<li>
<a class="submenu-pages__link" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
child_of
(和depth
(与wp_list_pages()
一起使用;而不是作为WP_Query
类的参数的一部分。
因此,要使代码正常工作(new WP_Query()
部分(,您所需要做的就是使用post_parent
来检索指定父级的子级帖子:
$args = array(
'post_parent' => $post->ID, // use post_parent and not child_of
'post_type' => 'page',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'date',
);