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

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • WP doesn’t do it out of the box… but
    https://www.ads-software.com/search/show+children+pages?forums=1

    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>';
    }
    ?>

    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>';
    }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to show page children links ONLY when viewing parent’ is closed to new replies.