我试过勾引wp_nav_menu_args
获取导航菜单的ID。我试图使用javascript向容器菜单添加一个元素。但我从来没有拿到菜单的ID。
如何从以下位置向导航菜单容器添加元素:
<nav id="my-menu"><ul><li>.....<li></ul></nav>
收件人:<nav id="my-menu"><div class="additional"></div><ul><li>.....<li></ul></nav>
我试过勾引wp_nav_menu_args
获取导航菜单的ID。我试图使用javascript向容器菜单添加一个元素。但我从来没有拿到菜单的ID。
如何从以下位置向导航菜单容器添加元素:
<nav id="my-menu"><ul><li>.....<li></ul></nav>
收件人:<nav id="my-menu"><div class="additional"></div><ul><li>.....<li></ul></nav>
假设您可以访问php文件,那么这相当简单。
在生成菜单的模板中,修改wp_nav_menu 调用以指定container = FALSE
, 并在周围的HTML中手动提供容器和附加div。
在事先不知道代码的情况下,一种方法是按如下方式修改代码:
<nav id="my-menu"><div class="additional"></div>
<?php
// Replicate the args from the original code for the most part
$args = array(
\'theme_location\' => \'primary\', // Or whatever the original was
\'container\' => FALSE, // This tells it to not put a container around it
\'echo\' => TRUE, // again, whatever original was
// .. any other args from original code ...
);
wp_nav_menu($args);
?>
</nav>
EDIT:知道菜单需要在生成时进行修改,然后答案会发生一些变化:您需要创建custom menu walker
基本原理如下。根据您的需要进行修改,请参考上面的Walker文章:
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Copy all the start_el code from source, and modify
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
// Copy all the end_el code from source, and modify
}
}
然后,在主题文件中,您可以调用wp\\u nav\\u菜单,如下所示:wp_nav_menu(array(
\'theme_location\' => \'main\',
\'container\' => false,
\'menu_id\' => \'nav\',
\'depth\' => 1,
// This one is the important part:
\'walker\' => new Custom_Walker_Nav_Menu
));
在菜单上,我说的是自定义菜单,我有一个带有几列的页脚,每列都有一个项目列表,每列菜单都需要从wp admin上的菜单选项进行更新,所以我创建了关于函数的部分。调用菜单并在页脚上添加每个菜单的php。php文档,类似于:<?php wp_nav_menu( array(\'menu\' => \'f-1\', \'menu_class\' => \'\', \'container\' => \'nav\' )); ?> <?php wp_nav_menu( arr