如何为菜单设置自定义标记,就像我想给菜单项id="menuitem-postname"
我该怎么做?
基于页面的带有wp_NAV_MENU的自定义标记
3 个回复
最合适的回答,由SO网友:PNMG 整理而成
我能想到的唯一方法是在wp_nav_menu_items
. 下面是我要处理的代码(只需将其添加到您的themes functions.php文件中):
add_filter(\'wp_nav_menu_items\', \'my_id_name_nav\', 10, 2);
function my_id_name_nav($items,$args) {
if( $args->theme_location == \'primary\' ) {
preg_match_all(\'/<li id="menu-item-([0-9]+)"([^>]+)>\\s*<a href="([^"]+)">([^<]+)<\\/a><\\/li>/i\', $items, $matches);
$newItems = array();
for($i=0; $i<count($matches[0]); $i++){
$short = explode(\'/\', $matches[3][$i]);
$short = $short[count($short) - 2]; // if there is no trailing slash, subtract 1 instead of 2
array_push($newItems, \'<li id="menu-item-\'. $short .\'"\'. $matches[2][$i] .\'><a href="\'. $matches[3][$i] .\'">\'. $matches[4][$i] .\'</a></li>\');
}
return implode(\'\', $newItems);
} else {
return $items;
}
}
它使用url的最后一部分来创建id的页面名称http://yoursite.com/page1/page2/ 将成为menu-item-page2。这个if( $args->theme_location == \'primary\' )
用于针对特定菜单。如果要对所有菜单执行此操作,只需删除If else语句。
SO网友:Edward J Beckett
非常简单。。。使用自定义Walker。。。您所需要做的就是将某个常量附加到id元素。。。例如实际的帖子或页面id。。。在菜单项->对象id中。。。我真的不明白你为什么这么做。。。通过对CSS的粗略了解,您可以轻松地利用级联来处理子项。。。所有菜单项都已分配了css类@看见the Codex
$args = array (
\'theme_location\' => \'primary\',
\'menu\' => \'Menu Name\',
\'container\' => \'div\',
\'container_class\' => \'nav\',
\'container_id\' => FALSE,
\'menu_class\' => FALSE,
\'menu_id\' => FALSE,
\'echo\' => TRUE,
\'fallback_cb\' => \'wp_page_menu\',
\'before\' => FALSE,
\'after\' => FALSE,
\'link_before\' => FALSE,
\'link_after\' => FALSE,
\'items_wrap\' => "\\n\\t" . \'<ul>%3$s</ul>\' . "\\n",
\'depth\' => 0,
\'walker\' => new ID_Walker()
);
wp_nav_menu( $args );
class ID_Walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
/**
* Add the object id as a reference
* This will tie your menu_item id ref to your post page or cpt
*/
$oid = esc_attr( $item->object_id );
/**
* Append the object id to the element id
*/
$id = $id ? \' id="\' . esc_attr( $id ) . \'-\' . $oid . \'"\' : \'\';
$output .= $indent . \'<li\' . $id . $value . $class_names .\'>\';
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
$attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
$attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
$attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\';
$item_output = $args->before;
$item_output .= \'<a\' . $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
SO网友:Asbjørn Ulsberg
只需将自定义函数添加到nav_menu_item_id hook 这样做:
function custom_nav_menu_item_id($menu_id, $item, $args, $depth) {
if ($item) {
switch ($item->object) {
case "page":
// If the menu item\'s object is a page,
// retrieve it and return its name.
$post = get_post($item->object_id);
return $post->post_name;
case "category":
// If the menu item\'s object is a category,
// retrieve it and return its slug.
$category = get_category($item->object_id);
return $category->slug;
}
}
// Return the standard menu id by default.
return $menu_id;
}
add_filter(\'nav_menu_item_id\', \'custom_nav_menu_item_id\', 10, 2);
如果菜单项类型多于page
和category
, 很容易添加case
每个模块的块。结束