WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List Whole Subpages (3 posts)

  1. Krissy
    Member
    Posted 1 year ago #

    How would I go about altering the "List Whole Subpages" reference to display the level 1 name as a link?

    <?php
    if(!$post->post_parent){
    	// will display the subpages of this top level page
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    }
    else{
    
    	if($post->ancestors) {
    		// now you can get the the top ID of this page
    		// wp is putting the ids DESC, thats why the top level ID is the last one
    		$ancestors = end($post->ancestors);
    		$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
    	}
    }
    
    if ($children) { ?>
    	<ul>
    		<?php echo $children; ?>
    	</ul>
    <?php } ?>

    This is the code off of http://codex.wordpress.org/Function_Reference/wp_list_pages#List_whole_subpages

    I altered it so that the Title actually would display but all it outputs is "Pages", which is the complete opposite of what I need.

    I need the output to be:

    Parent Page Title (Level 1)(as link)
    - Child (Level 2)
    -- Grandchild (Level 3)

    I need the format to remain the same while on Parent / Child or Grandchild. Now the code above works for the actual output on how the sidebar displays the list... but how would I go about altering the code to it always displays the Parent Title as a link back to the parent page?

    I'm working on a site that has:

    About Us
    - Our Company
    - Board of Directors
    -- Director #1
    -- Director #2
    -- Director #3

    I need "About Us" to actually display on the top of the page-list as a link and constantly be linked back to /about-us/ but I can't hard-code the template because it's being used for multiple sections, all of which would need to reflect the main parent.

    I hope I'm making sense, I Googled for a good hour and couldn't come up with anything.

    Help??

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think this is what you want:

    if(!$post->post_parent){
             // will display the subpages of this top level page
             $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
             $parent_title = get_the_title();
             $parent_permalink = get_permalink($post->ID);
          }
          else{
    
    		   if($post->ancestors) {
    		      // now you can get the the top ID of this page
    		      // wp is putting the ids DESC, thats why the top level ID is the last one
    		      $ancestors = end($post->ancestors);
    		      $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
    		      $parent_title = get_the_title($ancestors);
    		      $parent_permalink = get_permalink($ancestors);
             }
          }
    
    		if ($children) { ?>
    			<h2><a href="<?php echo $parent_permalink; ?>"><?php echo $parent_title; ?></a></h2>
    		   <ul>
    		      <?php echo $children; ?>
    		   </ul>
    		<?php }
  3. Krissy
    Member
    Posted 1 year ago #

    It works perfectly!! Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic