I'm trying to insert a shortcode as the last item in a menu but it results in creating the <li> but then throwing the shortcode output elsewhere, outside of the list. It also removes the closing <li> for that menu item?

In functions.php (as per Jared's answer below)..

    // Add Cart Contents to End of Nav
    add_filter('wp_nav_menu_items','my_item', 11, 2);
    function my_item($items, $args) {
        $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-last" id="menu-item-last">' . do_shortcode('[shopping_cart empty_msg="You have no items in your basket"]') . '</li>';
        return $items;

The output is:

<div xmlns="http://www.w3.org/1999/xhtml" class="wrap"><div id="title-area"><p id="title"><a title="asdf" href="asdf">asdfy</a></p><p id="description">asdf</p></div><!-- end #title-area -->

You have no items in your basket

<div id="nav"><div class="wrap"><ul class="nav" id="menu-main-navigation-menu"><li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-11 current_page_item menu-item-91" id="menu-item-91"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29" id="menu-item-29"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2838" id="menu-item-2838"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-34" id="menu-item-34"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31" id="menu-item-31"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33" id="menu-item-33"><a href="asdf">asdf</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-112" id="menu-item-112"><a href="asdf">asdf</a></li>

<li id="menu-item-last" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-last"/></ul></div><!-- end .wrap --></div></div>

Edit: Cart66ShortcodeManager.php

   * Short code for displaying shopping cart including the number of items in the cart and links to view cart and checkout
  public function shoppingCart($attrs) {
    $cartPage = get_page_by_path('store/cart');
    $checkoutPage = get_page_by_path('store/checkout');
    $cart = Cart66Session::get('Cart66Cart');
    if(is_object($cart) && $cart->countItems()) {
      <div id="Cart66scCartContents" style="float:right; text-align: right;">
        <a id="Cart66scCartLink" href='<?php echo get_permalink($cartPage->ID) ?>'>
        <span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
        <span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? ' items' : ' item' ?></span> 
        <span id="Cart66scCartCountDash">&ndash;</span>
        <!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL . 
          number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
        <a id="Cart66scViewCart" href='<?php echo get_permalink($cartPage->ID) ?>'>View Cart</a>
        <span id="Cart66scLinkSeparator"> | </span>
        <a id="Cart66scCheckout" href='<?php echo get_permalink($checkoutPage->ID) ?>'>Check out</a>
    else {
      $emptyMessage = isset($attrs['empty_msg']) ? $attrs['empty_msg'] : 'Your cart is empty';
        //echo "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">$emptyMessage</p>";
        echo "$emptyMessage";
  • Moderators: Please remove this question as I will be throwing it over to StackOverflow. Thank you – iCeR Feb 28 '12 at 23:15
  • Actually, I can just move this over tho SO for you ... would you rather have me delete it or migrate it? – EAMann Feb 29 '12 at 5:53
  • @EAMann: EAMann, thank you! Probably best that the other one is deleted or migrated, it seems that Jared has provided an answer that I will accept. Just about to test it :) Thanks! – iCeR Feb 29 '12 at 6:44
  • If you've already got a good answer here, there's no need to migrate! :-) Good luck! – EAMann Feb 29 '12 at 14:51

The problem is the fact that you are trying to echo something into a string, echo should only be used to output something, so simply change the following line:

$items .= '<li class="myclass">' . echo do_shortcode('[shopping_cart]') . '</li>';

To this:

$items .= '<li class="myclass">' . do_shortcode('[shopping_cart]') . '</li>';

Edit: On another note, this is merely a PHP question, such questions should be asked at StackOverflow.

Edit 2: In regards to your last edit on your question, shortcode functions need to return data, not echo it, this is what is causing the issue. You would need to take everything you are echoing and put it into a variable instead, and then return that variable (which contains the HTML code you want to output).

 * Short code for displaying shopping cart including the number of items in the cart and links to view cart and checkout
public function shoppingCart( $attrs ) {

    $cartPage = get_page_by_path( 'store/cart' );
    $checkoutPage = get_page_by_path( 'store/checkout' );
    $cart = Cart66Session::get( 'Cart66Cart' );

    if( is_object( $cart ) && $cart->countItems() ) {

        $buffer = '<div id="Cart66scCartContents" style="float:right; text-align: right;">';
        $buffer .= '<a id="Cart66scCartLink" href="' . get_permalink( $cartPage->ID ) . '">';
        $buffer .= '<span id="Cart66scCartCount">' . $cart->countItems() . '</span>';
        $buffer .= '<span id="Cart66scCartCountText">' . $cart->countItems() > 1 ? ' items' : ' item' . '</span> ';
        $buffer .= '<span id="Cart66scCartCountDash">&ndash;</span>';
        $buffer .= '<!-- <span id="Cart66scCartPrice">' . CART66_CURRENCY_SYMBOL.number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2).'</span></a> -->';
        $buffer .= '<a id="Cart66scViewCart" href="' . get_permalink( $cartPage->ID ) . '">View Cart</a>';
        $buffer .= '<span id="Cart66scLinkSeparator"> | </span>';
        $buffer .= '<a id="Cart66scCheckout" href="' . get_permalink( $checkoutPage->ID ) . '">Check out</a>';
        $buffer .= '</div>';
        return $buffer;

    } else {

        $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
        return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";


  • Thank you Jared! You're right- just thought it would have something to do with the genesis framework. It worked, however not quite as it should, as it doesn't insert the shortcode output in the <li>, rather outside of it? I'll update my questions now.. – iCeR Feb 28 '12 at 9:08
  • @iCeR The new problem that has risen from this may in fact be a WordPress question. Can you update your question with the function that displays the [shopping_cart] shortcode? – Jared Feb 29 '12 at 3:41
  • Sure! Placing it in there now. You will find my commented out code where I stripped all styling so it just echo's the variable.. – iCeR Feb 29 '12 at 5:07
  • @iCeR See my last edit. :) – Jared Feb 29 '12 at 5:47
  • thank you Jared! Just to be sure I get you.. I now do: $cartoutput = echo do_shortcode('[shopping_cart empty_msg="You have no items in your basket"]'); and then $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-last" id="menu-item-last">' . $cartoutput . '</li>'; ? – iCeR Feb 29 '12 at 6:47

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.