Reputation: 18
Example, I've got this menu, and added to the pages About and Vacancies subpages, and added them in the page settings
Home
About
-- History
-- Photo's
Vacancies
-- Front-end developer
-- Full-stack developer
Contact
How can I get the current main page when I'm on a subpage? For example I'm on the main page About, than I want a list of all subpages (History and Photo's in this example)
The same for page vacancies, when I'm on the main page I want to get a list of all children pages.
I tried below, but then I get all pages of cpt 'page'. Not the children's that I need. Can anyone help please? :)
<?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(); ?>
Upvotes: 0
Views: 320
Reputation: 15620
child_of
(and depth
) are used with wp_list_pages()
; not as part of the arguments for WP_Query
class.
So all you need to do to make your code works (the new WP_Query()
part), is use post_parent
to retrieve posts that are children of the specified 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',
);
Upvotes: 1