向 WordPress 主题添加子菜单

发布于 2024-09-19 18:06:22 字数 1047 浏览 20 评论 0原文

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

我们假设菜单结构如下所示:

  • Home
  • Entry1
    • 条目3
    • 条目4
  • 条目2
    • 条目5
    • 条目6

我希望如果有人单击 Entry1(并将其设为父项),则主题仅显示该条目的子菜单。对于 Entry1 来说,它是:

  • 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

我正在寻找解决方案这个问题困扰了这么久所以请帮助我。多谢。

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 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-09-26 18:06:22

为了使其正常工作,我必须在页面加载后立即隐藏 .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();
)};

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();
)};
左岸枫 2024-09-26 18:06:22

这应该有帮助:来自 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 } ?>

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 } ?>
空‖城人不在 2024-09-26 18:06:22

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文