Galen Gidman

Front-end and WordPress developer

Adding Static Menu Items to wp_nav_menu()

This week I found myself needing to append a static menu item to the end of a wp_nav_menu()-powered navigation menu. After a bit of Googling, I discovered I could do this using wp_nav_menu()‘s items_wrap parameter.

It’s actually pretty simple. Create a function that picks apart the default value of items_wrap and rebuilds it with a static link. Then call that function in the items_wrap parameter of wp_nav_menu():

How would something like that be useful? In my case, I was adding a WooCommerce cart link to the navigation that dynamically pulled in the sub-total. My code looked something like this:

You can even check for certain conditions, and only return the static menu item along with the default items if the those conditions are true.

Here, I’m checking to see if there are items in the WooCommerce cart, and if there are, I’m adding a link to the cart to the nav menu.


      1. I’m not sure if this is what JMarqz is asking but I’m trying to figure it out.

        It seems like the above code places your cart link at the end of targeted menu.

        This makes sense for a cart link.

        However, how would I add a link to the beginning of a targeted menu. Meaning it shows up first in the HTML markup and unordered list?

        Or is that what you are explaining above in your reply?

      2. This code represents the menu items as defined in the WordPress admin: $wrap .= '%3$s';

        To put your static element at the beginning of the nav menu, put your custom code above ↑ that line, not after.

  1. Hi Galen

    Thanks, Great article! Helped me a lot.

    Just a quick question, what would I need to change in order to display a count of the number of items in the cart, instead of the total price?

    Thanks in advance.

  2. Hi, I have problem with this code. When I add items_wrap element it’s completely ignored by WP. I found tips that this could cause by cleanup.php which overwrite it. Unfortunatelly I don’t understand theirs solution. Could you help? Thanks!

  3. When using functions, they need to return intead of echo. Like:
    $wrap .= ”.woocommerce_page_title(false).”;

    But how to do use a widget? like:
    $wrap .= ” . the_widget( ‘WC_Widget_Product_Search’, array(‘echo’=>false, ‘title=’)) . ”;
    I cannot find an $echo argument for widgets…

    1. This is a good use case for output buffering:

      the_widget( 'WC_Widget_Product_Search' );
      $widget = ob_get_clean();
      $wrap .= $widget;

Leave a Reply

Your email address will not be published. Required fields are marked *