0

I'm trying to get the list of authors to automatically appear on the main navi (without having to manually add them one by one as they are created).

So far I added this on my functions.php, but no luck:

function wps_nav_authors ( $items, $args ) {
if ( $args->theme_location == 'Primary') {
$items .= '<li><a href="#">Authors</a><ul class="sub-menu"><li>' . wp_list_authors('show_fullname=1&optioncount=0&exclude_admin=0&orderby=post_count&order=DESC&number=8&echo=0') . '</li></ul></li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'wps_nav_authors', 10, 2 );

I've also tried with simpler codes just to see if I was able to add a random li element to the menu, but also without result. I tried with:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
if ($args->theme_location == 'Primary') {
$items .= '<li>Show whatever</li>';
}
return $items;
}

Also, my main menu is called Primary.

Any ideas? Thanks!

1

Just out of luck, I tried adding the code to nav.php instead of the custom.php (which contains the functions.php in the Roots.io theme), and it worked like a charm.

Also, being based on Boostrap 3, I had to add some classes and attributes for the dropdown to work. Here's the final code, if anybody else might need it:

function add_last_nav_item($items, $args) {
if (!is_admin() && $args->theme_location == 'primary_navigation') {
$items .= '<li><a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">Authors <b class="caret"></b></a><ul class="dropdown-menu"><li>' . wp_list_authors('show_fullname=1&optioncount=0&exclude_admin=0&orderby=post_count&order=DESC&number=8&echo=0') . '</li></ul></li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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