0

I search in Internet but i didn't find a good documented way how to add to item from menu a number , my current menu is like

Item1
Item2
Item3 ,

I'm calculating with WP_Query how many post for category i have , and within function to update a specific menu item like :

Item1(10) 
Item2(5) 
Item3(20)

Thank you a LOT ! :)

i finded a solution , but i don't know how to filter by specific menu , location

function wpa_filter_nav_menu_objects( $items){

foreach( $items as $item ){

    if( strtolower($item->title)=='sample page' ){
        $item->title = $item->title.'(10)';
    }
}
return $items; } add_filter( 'wp_nav_menu_objects', 'wpa_filter_nav_menu_objects' );

menu i'm creating in admin panel , from Apperance > Menus

Thank you again !

2
  • Can you add more detail to your question?
    – Jake Lisby
    Jul 5, 2013 at 21:34
  • In order to get valuable help you have to provide us with your current code. There are multiple ways to generate a list/menu...
    – tfrommen
    Jul 5, 2013 at 21:52

1 Answer 1

0

You can target the location like this:

function wpa_filter_nav_menu_objects( $items, $args ){
    if( $args->theme_location === 'primary' ){
        foreach( $items as $item ){
            if( strtolower( $item->title ) === 'sample page' ){
                $item->title = $item->title . '(10)';
            }
        }
    }
    return $items;
}
add_filter( 'wp_nav_menu_objects', 'wpa_filter_nav_menu_objects', 10, 2 );

You can also try $args->menu_id if you use that input parameter.

2
  • sorry but $args , i tried but is not working , thank you for help !!!
    – mIRU
    Jul 9, 2013 at 10:29
  • ok, but this worked on my install, how do you call wp_nav_menu and what is the theme location?
    – birgire
    Jul 9, 2013 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.