开发者

Add submenu to Wordpress theme

I want to add a submenu of a wordpress menu into my theme. I want to use the wp_nav_menu function of Wordpress 3.0. And in other words, I want to see the submenu not the subpages which means that wp_list_pages is not the right function because I want the submenu and not the subpages.

Let's assume the menu structure looks like that:

  • Home
  • Entry1
    • Entry3
    • Entry4
  • Entry2
    • Entry5
    • Entry6

I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. In the case of Entry1 it's:

  • Entry3
  • Entry4

I know that there is a code like that:

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

However, the point is that I'm talking about the m开发者_如何学Cenu structure and not the page structure. Oh, and the depth parameter does not work because it means to here and not from here.

I think there could be a solution with a custom walker but I don't know how to implement that.

Function reference for wp_nav_menu http://codex.wordpress.org/Template_Tags/wp_nav_menu

I'm looking for a solution for this problem for so long so please help me. Thanks a lot.


In order to get this to work I had to hide the .sub-menu as soon as the page loaded. Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu"

$(document).ready(function() {
        $(".sub-menu").hide(); // hide the submenu on page load
    $(".current_page_item .sub-menu").show();
)};


This should help: From http://www.svennerberg.com/2009/02/creating-a-submenu-in-wordpress/

<?php
$has_subpages = false;
// Check to see if the current page has any subpages
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
    $has_subpages = true;
}
// Reseting $children
$children = "";

// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
if(is_page() && $post->post_parent) {
    // This is a subpage
    $children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
    // This is a parent page that have subpages
    $children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}
?>
<?php // Check to see if we have anything to output ?>
<?php if ($children) { ?>
<ul class="submenu">
    <?php echo $children; ?>
</ul>
<?php } ?>


One solution is to put another wp_nav_menu function on page and to modify css to hide inactive menu items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜