WordPress.org

Ready to get started?Download WordPress

Forums

How to show page children links ONLY when viewing parent (4 posts)

  1. palamedes
    Member
    Posted 7 years ago #

    Hey gang. Is there a way to make a child pages link only show up when the user is viewing the parent page?

    Thanks!

  2. moshu
    Member
    Posted 7 years ago #

    WP doesn't do it out of the box... but
    http://wordpress.org/search/show+children+pages?forums=1

  3. robertlove
    Member
    Posted 7 years ago #

    Yes.

    In your Page template file (page.php), get the ID of the parent page (this code must be in the loop):

    <?php $page_id = get_the_ID(); ?>

    Then, wherever you want to output the list of links to child pages:

    <?php
    $page_children = get_page_children($page_id, '');
    if (is_array($page_children)) {
        echo '<ul>';
        foreach ($page_children as $child) {
            echo '<li><a href="' . $child->guid . '">' . $child->post_title . '</a></li>';
        }
        echo '</ul>';
    }
    ?>
  4. robertlove
    Member
    Posted 7 years ago #

    Slight correction to last post!

    You don't need to get_the_ID in the loop, and you should check if $page_children is empty, not an array. So, with that said:

    <?php
    $page_children = get_page_children(get_the_ID(), '');
    if (!empty($page_children)) {
        echo '<ul>';
        foreach ($page_children as $child) {
            echo '<li><a href=\"' . $child->guid . '\">' . $child->post_title . '</a></li>';
        }
        echo '</ul>';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic