将子菜单添加到Wordpress主题

将子菜单添加到Wordpress主题

问题描述:

我想添加一个wordpress菜单的子菜单到我的主题中。我想使用Wordpress 3.0的wp_nav_menu功能。换句话说,我想查看子菜单而不是子页面,这意味着wp_list_pages不是正确的功能,因为我需要子菜单而不是子页面。将子菜单添加到Wordpress主题

假设菜单结构看起来像这样:

  • 首页
  • 条目1
    • Entry3
    • Entry4
  • ENTRY2
    • Entry5
    • Entry6

我想,如果有人点击条目1(并使其母公司)的主题只是显示这个条目的子菜单。在条目1的情况下,它是:

  • Entry3
  • Entry4

我知道有一个这样的代码:

<?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>'); } 
?> 

然而,问题是,我说起菜单结构和不是的页面结构。哦,深度参数不起作用,因为这意味着此处而不是这里。

我认为可以有一个解决方案与自定义步行者,但我不知道如何实现。对于wp_nav_menu http://codex.wordpress.org/Template_Tags/wp_nav_menu

功能参考我在寻找这个问题的解决方案了这么久,所以请帮助我。非常感谢。

+0

您是否找到了解决方案?我也有问题。 – Bajlo 2012-04-09 11:12:25

为了得到这个工作,我不得不在页面加载后隐藏.sub菜单。然后,按目标“.current_page_item .sub-menu”仅显示相关子菜单。

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

不错的想法在js而不是服务器上做。这不是我想要的,但会起作用。我接受这个,直到有人想出另一个解决方案;-) – dominik 2012-09-15 23:12:19

这应有助于:从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 } ?> 
+0

感谢您的帮助,但这不是我正在寻找的,因为代码显示的是子页面,而不是子导航。 – dominik 2010-09-17 14:50:25

一种解决方案是把另一个wp_nav_menu功能页上,并修改CSS来隐藏不活动的菜单项。