How to Display Top 8 Authors in wp_nav_menu Using wp_list_authors

Home » Snippets » How to Display Top 8 Authors in wp_nav_menu Using wp_list_authors

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to showcase the top 8 authors on your WordPress website? By default, WordPress provides a function called wp_list_authors that allows you to display a list of all authors registered on your site. However, it doesn’t provide a way to limit the number of authors shown or sort them based on their popularity. In this article, we will walk you through the process of using wp_list_authors along with some additional code to display only the top 8 authors in your navigation menu. So let’s dive in and learn how to showcase your most influential authors to your readers.

					function wpturbo_display_top_authors_in_menu($items, $args) {
    if($args->theme_location == 'primary') {
        $top_authors = wpturbo_get_top_authors(8); // Replace with your logic to get top authors
        $authors_menu = '';
        foreach($top_authors as $author) {
            $author_name = $author->display_name;
            $author_url = get_author_posts_url($author->ID);
            $authors_menu .= '<li><a href="' . $author_url . '">' . $author_name . '</a></li>';
        $items .= $authors_menu;
    return $items;
add_filter('wp_nav_menu_items', 'wpturbo_display_top_authors_in_menu', 10, 2);

The code snippet above shows how to display the top 8 authors in the WordPress navigation menu using the wp_list_authors function.

First, we define a new function called wpturbo_display_top_authors_in_menu that takes two parameters: $items and $args. These parameters are provided by the wp_nav_menu_items filter and represent the list of menu items and the menu object, respectively.

Within the function, we check if the menu location is ‘primary’ using the $args->theme_location property. This condition ensures that the code snippet only modifies the primary menu.

Then, we call the wpturbo_get_top_authors function to retrieve the top authors. You would need to replace this function call with your own logic to determine the top authors based on your criteria. The function should return an array of author objects.

Next, we initialize an empty string variable called $authors_menu to store the HTML markup for the menu item of each author.

We loop through each author in the $top_authors array using a foreach loop. Within the loop, we retrieve the display name of the author using $author->display_name. We also get the URL for the author’s posts using the get_author_posts_url function and pass the author’s ID as the parameter.

We then concatenate the HTML markup for the menu item of each author to the $authors_menu variable. The markup consists of an <li> element with an <a> element inside, with the author’s URL as the href attribute and the author’s name as the link text.

After the loop, we append the $authors_menu to the $items variable, which represents the existing menu items. This ensures that the menu items for the top authors are added to the navigation menu.

Finally, we return the modified $items variable.

To enable this functionality, we use the add_filter function to hook the wpturbo_display_top_authors_in_menu function to the wp_nav_menu_items filter with a priority of 10 and a parameter count of 2.

Now, when the wp_nav_menu function is called to render the primary menu, the wpturbo_display_top_authors_in_menu function will be triggered, and the top authors will be added as menu items.

Register an account to save your snippets or go Pro to get more features.