WordPress.org

Ready to get started?Download WordPress

Forums

Child Pages Code (advanced?) (2 posts)

  1. educationthemes
    Member
    Posted 5 years ago #

    I am trying to reduce the number of files in my theme, and hopefully prevent users from having to choose between page templates to make sure the pages display correctly. Therefore this is what i want to do in my sidebar.php but am struggling:

    I want some code that will do the following:

    Check to see if the page has child pages.
    If so display them as an unordered list.
    If the page is a child page and doesn't have any child pages of its own, then display other child pages of the parent page in an unordered list.
    If it doesn't have child pages then list the categories (top level) and archives (monthly).

    That is basically the 'English' of what I would like to do and I was wondering whether someone could give me the 'php' code.

    I have looked in the codex at conditional tags and some plugins but i have not managed to crack it after 3 hours of struggle.

    Thanks in advance.

  2. educationthemes
    Member
    Posted 5 years ago #

    Ok well i have used this code below from the codex wp_list_pages:

    <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul><?php echo $children; ?>
    </ul>
    <?php } ?>

    This displays a parents children pages and also if you are on a child page it displays the other children.

    However what about when one of those child pages is also a parent to another page. This means that that those child pages are displayed. Let me try and show what i mean below:

    The above code works well for this strucure:

    -about
    --name
    --age

    If you are on about it displays the child pages 'name' and 'age'. If you are on one of the child pages 'name' or 'age' then it displays both child pages - 'name' and 'age'.

    However my structure is like below:

    -about
    --name
    ---forename
    ---surnamce
    --age

    Now when I click on 'name' page it doesn't display its children i.e. 'forename' and 'surname' it display the other child of its parent i.e. 'age'.

    How can I get it to display the children all the time and only display children of a parent page when they are no more children.

    Sorry that is so complicated but it is hard to explain in writing. Thanks for reading this far anyway and hope someone can help me.

Topic Closed

This topic has been closed to new replies.

About this Topic