WordPress.org

Support

Support » How-To and Troubleshooting » If Child Pages Help

If Child Pages Help

  • Hi there all.

    I am developing some themes and want to have a page whereby a list of child pages is displayed in the sidebar if the page has any child pages. If the page does have child pages this list will be shown in the sidebar on the parent page and the child pages. This is easy, as using this page in the codex:

    http://codex.wordpress.org/Template_Tags/wp_list_pages#List_Sub-Pages_.28Wordpress_2.0.1_or_newer.29

    has the code for that:

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

    What i want to be able to do is to modify this code so that if the page does not have child pages, then I can display an archive list (monthly). Must be something to do with elseif? but I can’t quite get my head round it. Any help would be greatly appreciated.

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Vaughan

    @vaughan-van-dyk

    Hi,

    That example will never satisfy an else condition because it will always contain a list of child pages. But modifying an example listed just above that one on that page, I could get the list of child pages if on a parent page (else it displays a monthly archive if on a child page):

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

    Hope that helps.
    Vaughan

    Thanks for that I will give it a try

    That works a treat, however the archive list is not wrapped in a <ul> and therefore doesn’t look right and also won’t validate correctly.

    I add a ul tage then I get errors. Any ideas?

    I’m having a similar issue.

    Ever since my webhost changed servers, my child pages will not show up.

    In other words, when I’m on a parent page (except for the homepage) I want the child pages to appear on both the parent and the child pages.

    Here’s the code I had before the switch:

    <?php
    if($post->post_parent)
    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0&exclude=314,317,422,425,427,430,438″); else
    $children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&echo=0&exclude=314,317,422,425,427,430,438″);

    if ($children) { ?>

      <?php

      if (is_page(2) || $post->post_parent==”2″) {
      echo ‘

    • <h2>About</h2>
    • ‘;

      }

      else if (is_page(50) || $post->post_parent==”50″) {
      echo ‘

    • <h2>Services</h2>
    • ‘;
      }

      else if (is_page(393) || $post->post_parent==”393″) {
      echo ‘

    • <h2>eCourses</h2>
    • ‘;
      }

      else if (is_page(34) || $post->post_parent==”34″) {
      echo ‘

    • <h2>Portfolio</h2>
    • ‘;

      }

      else {
      echo ”; // Fall-through
      }

      ?>
      <?php echo $children; ?>

    <?php } ?>

    However, when I do list pages, it lists all the pages.

    <?php wp_list_pages(‘exclude=314,317,422,425,427,430,438&title_li=<h2>’ . __(‘Pages’) . ‘</h2>’ ); ?>

    I just don’t know what else to so,

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘If Child Pages Help’ is closed to new replies.