$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom nav menu current item custom link problem|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom nav menu current item custom link problem

matteradmin11PV0评论

Because I want to add <span class="dashicons [class]"></span> inside each link of my menu I needed to create a custom made nav menu. I did this by the code below which works great, but there is 1 problem.

I use custom links for showing post type archives in the menu. And I make a comparison between the page's post ID and the object ID of an item to check if the item is the current page. But with custom links, the object ID does not exists, so I cannot make the comparison.

$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;
}

Because I want to add <span class="dashicons [class]"></span> inside each link of my menu I needed to create a custom made nav menu. I did this by the code below which works great, but there is 1 problem.

I use custom links for showing post type archives in the menu. And I make a comparison between the page's post ID and the object ID of an item to check if the item is the current page. But with custom links, the object ID does not exists, so I cannot make the comparison.

$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;
}
Share Improve this question asked Nov 4, 2015 at 16:54 RobbertRobbert 1,2756 gold badges23 silver badges47 bronze badges 2
  • I'd be surprised if this couldn't be done with all CSS, given that you are adding the span to all links. – s_ha_dum Commented Nov 4, 2015 at 17:21
  • @s_ha_dum I don't think so, because I need the classes from the menu items from database. – Robbert Commented Nov 4, 2015 at 18:53
Add a comment  | 

1 Answer 1

Reset to default 0

I had to compare the $object_id with the current queried object id instead of the post id. https://www.robbertvermeulen/wp_get_nav_menu_items-current-menu-item/

$object_id  = get_post_meta( $id, '_menu_item_object_id', true );
$current    = ( $object_id == get_queried_object_id() ) ? 'current-menu-item' : '';

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far