WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Where do I place the PHP code to insert or not insert a menu item based upon some condition, i.e. value of another PHP variable like so:

$listItem = '';

<?php if ($condition)
    $listItem = '<li id="menu-item-44" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-44"><a title="Our Real Estate Blog" href="http://www.mattepstein.com/category/blog/">Blog</a></li>';


/* html for menu list */

<?php echo $listItem; ?>

/* more html for menu list */
share|improve this question

Use wp_get_nav_menu_items to iterate through the menu items, modifying the one that you want to in the loop. See the example code here for more information.

share|improve this answer

If the menu is displayed via regular code (wp_nav_menu() for example), you should not call wp_get_nav_menu_items() yourself, but you should hook into it and change the output. That means you add a filter on wp_get_nav_menu_items, loop over the items, and insert your menu item at the right place.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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