因为我想添加<span class="dashicons [class]"></span>
在菜单的每个链接中,我需要创建一个定制的导航菜单。我通过下面的代码做到了这一点,它非常有效,但有一个问题。
我使用自定义链接在菜单中显示post类型存档。我将页面的post ID和项目的对象ID进行比较,以检查项目是否为当前页面。但对于自定义链接,对象ID不存在,因此无法进行比较。
$location = \'header-navigation\';
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[$location] ) ) {
global $post;
$post_id = $post->ID;
$menu = wp_get_nav_menu_object( $locations[$location] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
$menu_list = \'<nav class="site-navigation">\';
$menu_list .= \'<ul id="menu-\' . $location . \'" class="grid-container">\';
foreach( ( array ) $menu_items as $key => $menu_item ) {
$id = $menu_item->ID;
$title = $menu_item->title;
$classes = $menu_item->classes;
$object_id = get_post_meta( $id, \'_menu_item_object_id\', true );
$current = ( $object_id == $post_id ) ? \'current-menu-item\' : \'\';
$menu_list .= \'<li id="menu-item-\' . $id . \'" class="\' . $current . \'"><a href="\' . $menu_item->url . \'"><span class="dashicons \' . $classes[0] . \'"></span>\' . $title . \'</a></li>\';
}
$menu_list .= \'</ul>\';
$menu_list .= \'</nav>\';
echo $menu_list;
}