• Resolved Jason

    (@jasoncamp)


    Hopefully a very simple tweak required here:

    The following sidebar script is doing ALMOST exactly what I want:

    • On top-level pages with no children, it displays nothing. This is key.
    • On top-level pages with children AND on its child pages, it lists the parent page name and links to the other children under that parent.

    The change I would like is this: instead of simply listing the parent page name, I would like a live link to that parent. This is probably remedial but it’s baffled me.

    <?php
    /* if the current pages has a parent, i.e. we are on a subpage */
    if ( $post->post_parent) {
    	$parent_title = get_the_title($post->post_parent);
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");			// list the parent page
    	$children = wp_list_pages("depth=1&title_li=&child_of=".$post->post_parent."&echo=0");	// append the list of children pages to the same
    }																							// $children variable
    
    /* else if the current page does not have a parent, i.e. this is a top level page */
    else {
    	$parent_title = get_the_title($post->ID);
    	$children = wp_list_pages("title_li=&include=".$post->ID."&echo=0");
    	$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page
    }
    
    /* if we ended up with any pages from the queries above */
    //if ($parent_title) {
    //	echo $parent_title;
    //} 
    
    if ($children) {
    	echo "<div style='width: 220px; background: #e8e0cb; padding: 25px; height: 100%;'>";
    	echo $parent_title;
    	echo "<ul style='list-style: none; padding-left: 20px;'>";
    	echo $children; /*print list of pages*/
    	echo "</ul>";
    	echo "</div>";
    } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Jason

    (@jasoncamp)

    I think I figured it out.

    <!-- subnavigation (if children exist) -->
    <?php
    /* if the current pages has a parent, i.e. we are on a subpage */
    if ( $post->post_parent) {
    	$parent_title = get_the_title($post->post_parent);
    	$parent_link = get_permalink($post->post_parent);
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");			// list the parent page
    	$children = wp_list_pages("depth=1&title_li=&child_of=".$post->post_parent."&echo=0");	// append the list of children pages to the same
    }																							// $children variable
    
    /* else if the current page does not have a parent, i.e. this is a top level page */
    else {
    	$parent_title = get_the_title($post->ID);
    	$children = wp_list_pages("title_li=&include=".$post->ID."&echo=0");
    	$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page
    }
    
    if ($children) {
    	echo "<div style='width: 220px; background: #e8e0cb; padding: 25px; height: 100%;'>";
    	echo "<a href='" . $parent_link . "'>" . $parent_title . "</a>";
    	echo "<ul style='list-style: none; padding-left: 20px;'>";
    	echo $children; /*print list of pages*/
    	echo "</ul>";
    	echo "</div>";
    } ?>

    Drawing on what you have here, @jasoncamp, in combination with the information listed in the Get Pages Function Reference page in the Codex, here’s another solution. (See wp list pages in the Codex for more on its parameters, including the ability to exclude specific pages.)

    <?php
         if($post->post_parent) { // if the current page has a parent
              $parent_title = get_the_title($post->post_parent);
              $parent_link = get_permalink($post->post_parent);
              $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");?>
              <ul>
              <li><?php echo "<a href='" . $parent_link . "'>" . $parent_title . " Main</a>"; ?>
              </li>
    
    <?php } else { // if the current page is the parent
              $parent_title = get_the_title($post->ID);
              $parent_link = get_permalink($post->ID);
              $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); ?>
              <ul>
                   <li class="current_page_item"><a href="#"><?php the_title(); ?> Main</a>
                   </li>
    <?php }
    	echo $children; ?>
            </ul>

    Hi,
    thanks for the code. But i was wondering if you could help this.

    this is my structure

    category1 -> parent -> child

    I’m currently on child page and want to list all available parents only (without listing of other childs) under this specific category1.

    thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Parent children in sidebar’ is closed to new replies.