简体   繁体   English

Wordpress:在wp_nav_menu中为ul添加自定义ID

[英]Wordpress: add custom ID to ul in wp_nav_menu

I am trying to alter the wp_nav_menu to output the html like the below example. 我正在尝试更改wp_nav_menu以输出html,如下例所示。

<div class="menu">
<ul id="menu">

The original output 原始输出

<div class="menu">
<ul>

I cant do it with jQuery or javascript, Its have to be php code 我不能使用jQuery或javascript,它必须是php代码

wp_nav_menu accepts the key menu_id in its options array. wp_nav_menu接受其options数组中的menu_id键。 Set it to the ID you want, eg: 将其设置为您想要的ID,例如:

wp_nav_menu(array(
    'menu_id' => 'menu'
));

Giving the ul an id that's the same as the class of its container is asking for trouble, but this should work: 给ul一个与其容器类相同的id就是在寻找麻烦,但这应该有效:

<?php

function showMenu(){
$args = array(
  'menu_id' => 'menu'
);

wp_nav_menu($args);

}

showMenu();
?>

The WordPress Codex has a page detailing all options for the wp_nav_menu() function: http://codex.wordpress.org/Function_Reference/wp_nav_menu WordPress Codex有一个页面,详细说明了wp_nav_menu()函数的所有选项: http//codex.wordpress.org/Function_Reference/wp_nav_menu

You can explicitly set the id in the html by defining items_wrap , and make sure walker is not set to some custom function: 您可以通过定义items_wrap在html中显式设置id,并确保walker未设置为某个自定义函数:

wp_nav_menu( array(
    'theme_location' => 'main-menu'
    'items_wrap'     => '<ul id="menu" class="%2$s">%3$s</ul>',
    'walker'         => '',
)); 

This is incomplete info; 这是不完整的信息; 1st attempt to use: 第一次尝试使用:

'fallback_cb'     => false,

If you menu doesn't appear, that means you have not created any menu and that means its taking the fallback function take care for that. 如果您的菜单没有出现,这意味着您没有创建任何菜单,这意味着它采取后备功能会照顾它。

So go and create a menu first. 因此,首先创建菜单。 :D :d

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

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