displaying the function result on a menu

I was wondering if it’s possible to display value returned from a function on the Main Menu in WordPress? I figured I can use custom links to display text on the menu. Now I would like call a function which calculates the number of users online and display the result on the menu.

Something like chinesepod.com does

Read More

Here’s the code for calculating the number of users : –

function ray_number_online_users() {
     $i = 0;

     if ( bp_has_members( ‘user_id=0&type=online&per_page=999&populate_extras=0′ ) ) :

       while ( bp_members() ) : bp_the_member();
           $i++;

       endwhile;   

     endif;

    return $i;
 }`

Related posts

1 comment

  1. The easiest way to target a single link on your menu is by giving it a class (user-number in this case).

    The theme location is defined by the register_nav_menus function

    register_nav_menus( array(
            'primary' => __( 'Primary Menu',      'twentyfifteen' ),
            'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
        ) );
    

    Here I target the primary menu location, but I could also target the social menu.

    function so30559666_nav_description( $item_output, $item, $depth, $args ) {
        if ( 'primary' == $args->theme_location && in_array("user-number", $item->classes)) {
            $count = ray_number_online_users();
            $item_output = str_replace( $args->link_after . '</a>', '<div class="menu-user-count">' . $count . '</div>' . $args->link_after . '</a>', $item_output );
        }
    
        return $item_output;
    }
    add_filter( 'walker_nav_menu_start_el', 'so30559666_nav_description', 10, 4 );
    

Comments are closed.