0

I'm currently listing WordPress pages and child pages on a side widget using the following code.

<ul class="side-links">
  <?php wp_list_pages( array( 

    'title_li' => '' 

   ) );
  ?>
</ul>

This lists all of my WordPress pages, if I add the following code

'child_of' => $id

into the array I get the children on the parent page, If I go to a specific child page the list will not be displayed. I just need the list of child pages to be displayed on all pages including parent and child pages.

If it helps I've placed the following wp_list_pages function on this page in the following theme. https://github.com/holger1411/understrap/blob/master/loop-templates/content-page.php

2 Answers 2

0

I don't think there is a way to express that precisely in its arguments.

You could work around that by building array of page IDs which includes parent and children set as desired.

$pages = get_posts( [
    'post_parent' => $page_id,
    'post_type'   => 'page',
    'fields'      => 'ids',
] );

$pages[] = $page_id;

wp_list_pages( [
    'include' => $pages,
] );
0

Here is the code with help form @Rarst that shows children page links on both parent and child pages.

<?php 
    global $post;
    $children = get_pages( array( 'child_of' => $post->ID ) );
    $hasChild = (count( $children ) > 0 );

    $page_id = ($hasChild) ? $post->ID : wp_get_post_parent_id( $post->ID );

        wp_list_pages( array(
        'title_li'    => '',
        'child_of'    => $page_id,
    ) );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.