WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Submenu on submenu page. (4 posts)

  1. FummeL
    Member
    Posted 11 months ago #

    Hi,

    Right now i have done a submenu with the following code:

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

    The code displays and links submenu item pages. The problem is when i click on a submenu page i want the same menu on that page.

    Is there a way to write an IF statement that checks if the page is a submenu item, display the submenu for parent.

  2. esmi
    Forum Moderator
    Posted 11 months ago #

    I did something very similar in one of my themes. I used the following in functions.php:

    // Generate page tree
    function purplepastels_page_tree($this_page) {
    	$pagelist = '';
    	if( !$this_page->post_parent ) {
    		$children = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    		if( $children ) {
    			$pagelist .= '<li class="current_page_item"><a href="'.  get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
    			$pagelist .= '<ul>' . $children . '</ul>';
    			$pagelist .= '</li>';
    		}
    	}
    	elseif( $this_page->ancestors ) {
    		// get the top ID of this page. Page ids DESC so top level ID is the last one
    		$ancestor = end( get_post_ancestors($this_page) );
    		$pagelist .= wp_list_pages('title_li=&include='.$ancestor.'&echo=0');
    		$pagelist = str_replace('</li>', '', $pagelist);
    		$pagelist .= '<ul>' . wp_list_pages('title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
    	}
    	return $pagelist;
    }

    The used:

    if( function_exists( 'purplepastels_page_tree') && purplepastels_page_tree($post) != '' ) :?>
    <div class="sub-pages"><h3><?php _e('Pages in this section', 'purplepastels');?></h3><ul>
    <?php echo purplepastels_page_tree($post);?></ul></div>
    <?php endif;?>

    in the relevant template file.

  3. FummeL
    Member
    Posted 11 months ago #

    esmi you are the best ;)

  4. esmi
    Forum Moderator
    Posted 11 months ago #

    Hope it works for you. :-)

Reply

You must log in to post.

About this Topic