Support » Plugins » Hacks » code review on custom menu

  • I, I’m totally newbie on wordpress and I’m very glad to post for the first time πŸ™‚

    I really don’t know if it is the good place to post this question, so do not hesitate to remove, move or just let it live just right here.

    I was wondering how to get a pages menu in which you can display the sub pages only if the current post is a part of the top parent family branch.

    for exemple if I have that kind of websites pages :

    |- graphic
    | |- print
    | |- web
    |- dev
    | |-front
    | |-back
    |…

    Yes, it is a peace of crap example, but that is not the point.
    Let’s say that I wish to display submenu elements in the same ul hierarchical menu.
    I do not want them to be displayed separatly with a ul main navigation and later a separated ul subnavigation.

    All the roots menu elements has to be displayed by default, but I want to display the sub elements only if the current page is a part of the current family branch.

    for example, if I’m in the “graphic” page, the submenu of graphic will be displayed, but not the submenu of the “dev” page.
    If the current page is the “front” page, the submenu of the dev submenu elements will be displayed, but not the submenu of the “graphic” page.

    I’m just a noob, so I did not figure “how to” do it with the wp_list_pages() template function.

    So here is what I did :

    // to display website navigation
    function globalnav(){
    	//niv1 menu
    	// to get array of the roots menu pages
    	$pagesNiv1=get_pages("sort_column=menu_order&parent=0");
    	$menu.=set_menu_recusion($pagesNiv1, 0);
    	return $menu;
    }
    // recursive menu
    function set_menu_recusion($arrayToParse, $lvl){
    	//$arrayToParse = element to recurse
    	//$lvl = current level of navigation
    	global $wp_query;
    	$post=$wp_query->post; //get current post datas
    	$menu='';
    	//if at least on element in the array
    	if(count($arrayToParse)>0){
    		//set ul tag with proper className and id for the root element
    		$menu.='<ul class="menuNiv'.$lvl.'"'.($lvl===0 ? ' id="menu"' : '').'>';
    		// loop the array
    		for($i=0; $i<count($arrayToParse); $i++) {
    		  	$menu .= '<li>';
    		  	// set if we need a a tag or if the page is the current one
    			$menu .= (($post->ID == $arrayToParse[$i]->ID) ? '<span>' : '<a href="'.get_page_link($arrayToParse[$i]->ID).'">');
    			$menu .= $arrayToParse[$i]->post_title;
    			$menu .= (($post->ID == $arrayToParse[$i]->ID) ? '</span>' : '</a>');
    			//get the children pages of the current iterated page
    			$nivInf=get_pages("sort_column=menu_order&child_of=".$arrayToParse[$i]->ID);
    			//if there is sub-pages and if the top parent page id of the current
    			//iterated page is the same than the top parent page id of the
    			//current page called by user
    			if(count($nivInf)>0 && (get_top_parent($arrayToParse[$i]) === get_top_parent($post)))
    				// set_menu_recusion, increase lvl
    				$menu.=set_menu_recusion($nivInf, ++$lvl);
    
    			$menu .= '</li>';
    		}
    		$menu.='</ul>';
    	}
    	return($menu);
    }
    // get the top parent
    function get_top_parent($post){
    	if ($post->post_parent)	{
    		$ancestors=get_post_ancestors($post->ID);
    		$root=count($ancestors)-1;
    		$parent = $ancestors[$root];
    	} else {
    		$parent = $post->ID;
    	}
    	return($parent);
    }

    Ok so, from now, it is working and it’s cool
    i just need to call :

    echo globalnav();

    but, yes there is always a but πŸ™‚

    I got severals questions about the technics I did use to do it.
    the first one, am I right to use this kind of code in the present context ?

    global $wp_query;
    $post=$wp_query->post; //get current post datas

    Maybe it is not recommended or not secure, maybe there is a way to reset the global access of the $wp_query after, when I do not need it anymore ?
    Maybe It is not recommended to do that way because it could remains performance latencies ?
    My other question is : is it possible to do the same with wp_list_pages() template function ?

    Thank you all in advance.

  • The topic ‘code review on custom menu’ is closed to new replies.