Ready to get started?Download WordPress


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

  1. FummeL
    Posted 1 year ago #


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

                  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
                  if ($children) { ?>
                  <ul id="sub-menu">
                    <?php echo $children; ?>
              <?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 1 year 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
    Posted 1 year ago #

    esmi you are the best ;)

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Hope it works for you. :-)

Topic Closed

This topic has been closed to new replies.

About this Topic