简体   繁体   English

将子菜单添加到Wordpress主题

[英]Add submenu to Wordpress theme

I want to add a submenu of a wordpress menu into my theme. 我想在我的主题中添加一个wordpress菜单的子菜单。 I want to use the wp_nav_menu function of Wordpress 3.0. 我想使用Wordpress 3.0的wp_nav_menu函数。 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. 换句话说,我希望看到子菜单不是子页面,这意味着wp_list_pages不是正确的功能,因为我想要子菜单而不是子页面。

Let's assume the menu structure looks like that: 我们假设菜单结构如下:

  • Home
  • Entry1 条目1
    • Entry3 Entry3
    • Entry4 Entry4
  • Entry2 ENTRY2
    • Entry5 Entry5
    • Entry6 Entry6

I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. 我希望如果有人点击Entry1(并使其成为父级),主题只显示此条目的子菜单。 In the case of Entry1 it's: 在Entry1的情况下,它是:

  • Entry3 Entry3
  • Entry4 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 menu 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 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. 非常感谢。

一种解决方案是在页面上放置另一个wp_nav_menu函数并修改css以隐藏非活动菜单项。

In order to get this to work I had to hide the .sub-menu as soon as the page loaded. 为了使这个工作,我必须在页面加载后立即隐藏.sub菜单。 Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu" 然后,通过定位“.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/ 这应该有所帮助:来自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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM