WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] OHow to only list grandchildren when on their direct parent page (9 posts)

  1. smoo
    Member
    Posted 4 years ago #

    Hi,

    I am trying to find out if it's possible to list just the children of a page that is already a child, only when you are on that first child page;

    The site has 3 tiers, as follows:

    Home
    Work
    Profile
    Media
    -Radio
    --Programme1
    --Programme2
    -Print
    --Publication1
    --Publication2
    Contact

    When clicking on 'Media' I am listing the immediate 'Child' pages (Radio & Print), and have managed to *not* show the grandchildren at this stage, by using the following code:

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

    I'd like to show 'Programme1 & Programme2' only when clicking on 'Radio', and 'Publication1 & Publication2' only when clicking on 'Print'

    Is this possible?
    I'm sorry if it's been answered already, I have been looking through the forums for some time, but haven't found anyone asking quite the same question (I don't think!)

    Thank you in advance for any help.

  2. smoo
    Member
    Posted 4 years ago #

    Ok, have found the 'offset' parameter in
    http://codex.wordpress.org/Template_Tags/wp_list_pages#List_subpages_even_if_on_a_subpage
    but am unsure how to use wp_list_pages again to include it..
    Can anyone help please?

    Thanks very much

  3. mcrib
    Member
    Posted 4 years ago #

    I'm trying to do this exact thing. Please let me know if you find a solution. I will do the same.

    Thanks

  4. mcrib
    Member
    Posted 4 years ago #

    It may be a good idea to put some checks in here to make sure that the page has children, but this is working for me:

    $page = $post->ID;

    $children = wp_list_pages("title_li=&child_of=".$page."&echo=0");

    if ($children) {?>

    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

  5. smoo
    Member
    Posted 4 years ago #

    Ok, I seem to have done it!
    I'm sure there are more elegant ways of going about it, but as I'm not a fully fledged php coder, the following is the best I can do right now.

    So, to write out 3 separate levels of navigation as described above, you will need a different page template for each level.
    (See here to create a custom page template if you're not sure how to do this.)

    The first navigation list (here in a div called 'mainnav') is common to every page, and so I have included it at the bottom of header.php.

    Pages at this first level will use the default page template 'page.php'. (Note it's asking to only show pages to a depth of 1):

    <div id="mainnav">
    <ul>
    <?php wp_list_pages('sort_column=menu_order&title_li=&depth=1'); ?>
    </ul>
    </div>

    Next, any page that is a direct child of one of the 'mainnav' pages will use a new 'page' template, let's call it page-2nd-nav.php.

    This page will still call the header at the top, but will also have a second navigation list, here in a new div called 'subnav':

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

    Then finally, for any page that is a child of a subnav page (or a grandchild of a mainnav page) use a third page template: page-3rd-nav.php

    This page again calls header.php to write out the mainnav list, and includes the following code first to write out the subnav list, (note that it's different code from the previous subnav list because we're now another layer down):

    <?php if($post->ancestors)
    	{
    	$ancestors = end($post->ancestors);
    	$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0&depth=1");
    	}
    if ($children) { ?>
    	<div id="subnav">
    	<ul>
    		<?php echo $children; ?>
    	</ul>
    	</div>
    <?php } ?>

    Finally use the following code to write out the third navigation list:

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

    As I said, probably not the most elegant solution, I'm sure someone could write something better, but it seems to be working for me.
    Enjoy!
    -smoo

  6. agatha83
    Member
    Posted 4 years ago #

    can someone please help me to get the same result for my page?
    I'm totally new to php and have been trying for weeks now but only end up messing everything up completely :(

    <div class="menuheader"></div><div class="menucontent">
    <ul>
    <?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>
    </ul>
    </div>
    <?php
    }
    if ( function_exists('register_sidebar_widget') )
        register_sidebar_widget(__('Links'), 'widget_techdesigns01_links');	 
    
    function list_subpages_techdesigns01($return = 0) {
    global $wpdb, $post;
    $current_page = $post->ID;
    while($current_page) {
    $page_query = $wpdb->get_row("SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $current_page = $page_query->post_parent; }
    $parent_id = $page_query->ID;
    $parent_title = $page_query->post_title;
    if($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status = 'publish'")) {
    echo'<div class="menuheader"></div><div class="menucontent"><h5 class="menunav"><span>Navigation</span></h5><ul>';
    $html = wp_list_pages("child_of=$parent_id&depth=$depth&echo=".(!$return)."&title_li=0&sort_column=menu_order");
    echo'</ul></div>
  7. agatha83
    Member
    Posted 4 years ago #

    is there nobody who can help me with that???

  8. philkershaw
    Member
    Posted 4 years ago #

    Hello lads and lasses,

    I've been working on something similar to this. Currently I've only managed to get a menu working down to Parent->child level and am currently investigating how to extend it to grandchildren.

    global $post;
    $parents = get_pages('parent=0&exclude=2&sort_column=menu_order');
    $output = '<ul>';
    foreach ($parents as $parent) {
    	$output .= '<li';
    	if ( $parent->ID == get_the_ID() || $parent->ID == $post->post_parent ){
    		$output .= ' class="current_page_item"><a href="'. get_page_link($parent->ID) .'">'. $parent->post_title .'</a>';
    		$children = get_pages('child_of='.$parent->ID.'&sort_column=menu_order');
    		if ($children){
    			$output .= '<ul>';
    			foreach ($children as $child) {
    				if ( $child->ID == get_the_ID() ){
    					$output .= '<li class="current_page_item"><a href="'. get_page_link($child->ID) .'">'. $child->post_title .'</a></li>';
    				}else{
    					$output .= '<li><a href="'. get_page_link($child->ID) .'">'. $child->post_title .'</a></li>';
    				}
    			}
    			$output .= '</ul>';
    		}
    	}else{
    		$output .= '><a href="'. get_page_link($parent->ID) .'">'. $parent->post_title .'</a>';
    	}
    	$output .= '</li>';
    }
    $output .= '</ul>';
    echo $output;

    Just 1 thing to note - I exclude page 2 in the parents as I use this as a custom homepage. Other than that it's pretty generic code. Works in both 2.9.2 and 3.0-beta2

  9. davidandre
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic