Adding Class current_page_item

Go To StackoverFlow.com

5

I am working on a Wordpress-Design and i want to creat a Custom Menu.

$items = wp_get_nav_menu_items('Menu', array(
    'order'                  => 'ASC',
    'orderby'                => 'menu_order',
    'post_type'              => 'nav_menu_item',
    'post_status'            => 'publish',
    'output'                 => ARRAY_A,
    'output_key'             => 'menu_order',
    'nopaging'               => true,
    'update_post_term_cache' => false));
echo '<pre>'; print_r($items); echo '</pre>'; 
foreach($items as $item){
    echo '<div class="menu_entry"><a href="'.$item->url.'">'.$item->title.'</a></div>';
}

The problem is, i need the "current-page"-Class, which is WordPress creating - in the Standard Menu.

Any Ideas how to add this class?

2012-04-04 21:26
by Styler2go


13

You can do a compare on the current page / cat etc ID against the menu items object_id which is the ID of the page / category etc its linked to.

Something like (untested);

global $post;
$thePostID = $post->ID;
foreach($items as $item){

    if($thePostID === $item->object_id) {
        echo '<div class="menu_entry"><a href="'.$item->url.'" class="current-menu-item">'.$item->title.'</a></div>';
    }else{
        echo '<div class="menu_entry"><a href="'.$item->url.'">'.$item->title.'</a></div>';
    }

}
2012-04-05 01:49
by noponies
Thanks very much :- - Styler2go 2012-04-05 03:18
For some reason this only worked for me with a regular double equals (==) instead of the tripple equals - deweydb 2013-02-18 13:04
It will not work if you've picked Category menu item for example as objectid is catid then - pie6k 2014-08-15 10:09
@AdamPietrasiak seconded for custom link - Ethan C 2016-01-28 15:15
Look at the solution from @EthanC it is a lot nicer : - Simon Pollard 2017-07-10 15:27


12

Solution time:

WordPress's function that adds these classes is _wp_menu_item_classes_by_context(). This is called already when you use wp_nav_menu but not wp_get_nav_menu_items. Fortunately, the latter provides a filter so we can do it ourselves:

add_filter( 'wp_get_nav_menu_items', 'prefix_nav_menu_classes', 10, 3 );

function prefix_nav_menu_classes($items, $menu, $args) {
    _wp_menu_item_classes_by_context($items);
    return $items;
}
2016-01-28 15:39
by Ethan C
This is very useful - I take the $items and amend the classes with a switch statement allowing me to add in some more CSS friendly tag names for my front end developers. A lot nice than fighting with walkers : - Simon Pollard 2017-07-10 15:26
Thanks a lot, this is useful - Efbi 2018-09-07 08:08


1

Using the function get_queried_object_id(). This works fine in all the pages, including the Blog page.

See an example:

if ( $menu_items = wp_get_nav_menu_items( 'menu' ) ) {
   foreach ( $menu_items as $menu_item ) {
      $current = ( $menu_item->object_id == get_queried_object_id() ) ? 'current' : '';
      echo '<li class="' . $current . '"><a href="' . $menu_item->url . '">' . $menu_item->title . '</a></li>';
   }
}
2018-06-21 22:47
by Gabriel Stafoca
Ads