I'm trying to add categories and authors automatically into the custom menu as they are created (avoiding having to do it manually for each one).

I've modified Alex Mill's code and got to display a list of elements with the categories on the menu, buuut the problem is that the list of elements is outside the ul of the menu in itself. You can see what I mean here.

And here's my version of the code:

// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );

// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {

// Loop through the menu items looking for placeholder(s)
foreach ( $items as $item ) {

// Is this the placeholder we're looking for?
if ( '#latestpost' != $item->url )

// Get the categories
$latestpost = wp_list_categories( array(
'orderby' => name,
'show_count' => 0,
'exclude' => 3,
'title_li' => "",
) );

if ( empty( $latestpost ) )

// Replace the placeholder with the real URL
$item->url = get_permalink( $latestpost[0]->ID );

// Return the modified (or maybe unmodified) menu items array
return $items;

Any idea on how to place this generated list of elements inside the proper dropdown (in this case, TEST)?

Thanks! Eric

1 Answer 1


buuut the problem is that the list of elements is outside the ul of the menu in itself.

I think part of the problem might be that wp_list_categories() is in echo-mode by default so $latestpost is always empty.

Please try it with these parameters instead:

// Get the categories
$latestpost = wp_list_categories( array(
  'orderby'    => name,
  'show_count' => 0,
  'exclude'    => 3,
  'title_li'   => "",
  'echo'       => 0,
) );

You can see the default arguments of wp_list_categories() here in the Codex.

The problem with your code example is also that the output of wp_list_categories() is an HTML string, not an array. So this part: $latestpost[0]->ID has no meaning when $latestpost is an (empty) string.

  • Hi Birgire, the problem is not that they echo, is that they echo outside the main menu ul. I mean, they should appear, but just under ul li<-(this one being categories) ul li Jan 8, 2014 at 14:30
  • On the case of the Categories, I fixed the issue installing this plugin, then creating a parent category called "Categories", that contains all categories. On the custom menu, I just add the "Categories" button to the menu and click on the checkbox that makes all descendants from this element (which are all other categories) appear automatically. A rather tricky solution for what it should be a simple problem, but it works. Thanks Birgire also! Jan 8, 2014 at 14:45
  • good you found a solution, maybe you should add it as an answer and accept it. ps: when you have an echo inside a function with a return it usually gives you a similar problem as you described.
    – birgire
    Jan 8, 2014 at 14:53
  • I wanted to add is a solution but I don't have enough reputation! : ) Jan 8, 2014 at 15:15
  • ok - I added more explanations to my answer to hopefully show better where the problem is with the code example.
    – birgire
    Jan 8, 2014 at 15:18

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.