WordPress.org

Ready to get started?Download WordPress

Forums

Show child pages in sidebar based off of top level parent (code included) (1 post)

  1. adam533
    Member
    Posted 1 year ago #

    Hello,

    I am trying to show the child pages in the sidebar of a page based off the top level parent page. It should only show the first level of child pages and if there are none, then nothing will appear.

    I am using the following code right now, and it works for the most part. The one issue I am having (and am unable to solve myself) is to have it show just the first level of child pages based off the top level parent. This code does not do that as is. Instead if I were to be on a page that is two levels deep, then it shows that level of pages in the sidebar.

    How can I update this so it always shows the first level of child pages based off of the top level parent page?

    Thanks!

    <?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) { ?>
    		<li class="widget">
            	<h2 class="widgettitle">
    			  <?php
                    $parent_title = get_the_title($post->post_parent);
                    echo $parent_title;
                  ?>
                </h2>
          <ul>
          <?php echo $children; ?>
          </ul>
          </li>
          <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.