WordPress Menu: Fixed section menu for pages

With this PHP function you can create a hierarchical and navigational menu for your pages in WordPress. It will take the top parent page and keep it as a title, and then build their children pages keeping the hierarchy and the same markup used in wp_list_pages();

<?php
    if(!$post->post_parent){
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    }else{
        if($post->ancestors){
            $ancestors = end($post->ancestors);
            $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        }
    }
	if($post->post_parent != 0){
		$id_padre=$post->post_parent;
		$finder=1;
		while($finder==1){
			$padre=get_post($id_padre);
			if($padre->post_parent==0){
				$id_search=$padre->ID;
				$finder=0;
         	}else{
             	$id_padre=$padre->post_parent;
         	}
     	}
	}else{
	$padre = $post;
        $id_search=$post->ID;
    }
?>
<h4 class="pagetitle"><span><?php echo $padre->post_title; ?></span></h4>
<?php
    if ($children){
        echo'<div id="submenu">';
        echo'<ul>';
        echo $children;
        echo'</ul>';
        echo'</div>';
    }
?>

Thanks to Santiago Fica from Macrotech

Leave a Reply