2

I am trying to add static menu item to Wordpress Menu.I am using filter 'wp_nav_menu_items' for this in functions.php. It works but it doesn't put it under menu container tag.

function add_nav_menu_items( $items , $args ) { ?>
    <ul>
        <li><a href="#">PRODUCTS</a>
            <ul>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Sci-Fi')));?>">SCI-FI</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'scifi','showposts'=>-1,'order'=>'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Drama')));?>">Drama</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'drama','showposts'=>-1,'order'=>'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Horror')));?>">HORROR</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'horror','showposts'=>-1,'order'=> 'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
<?php return $items;}

add_filter( 'wp_nav_menu_items', 'add_nav_menu_items',10,2);

When i put the code online firebug show it like this

<div id="navigation_top">
    <div id="navmenu_top">
        <ul>  **-->> This is my static menu**
            <li>  
        </ul>
        <ul id="menu-menu-1" class="menu-container">  **-->> This is WP_nav_menu**
            <li class="type menu-item-object-page menu-item-587">
            <li class="type menu-item-object-page menu-item-122">
            <li class="type menu-item-object-page menu-item-121">
            **I want my menu appear here **  
        </ul>
    </div>
</div>

Is there anyway i can do this???

Thanks for any advice...

2 Answers 2

1

The reason your PRODUCTS menu is appearing first is because you're echoing it out to the page as part of the filter function. The expectation with filters is that your function receives an argument, $items in this case, modifies it and then returns it to be used by the core code.

In order to avoid having to rewrite the code that generates your PRODUCTS menu, we'll make use of the output buffer:

add_filter( 'wp_nav_menu_items', 'add_nav_menu_items', 10, 2 );
function add_nav_menu_items( $items, $args ) {
     ob_start(); // start the output buffer
     ?>
     <ul>
        <li><a href="#">PRODUCTS</a>
            <ul>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Sci-Fi')));?>">SCI-FI</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'scifi','showposts'=>-1,'order'=>'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Drama')));?>">Drama</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'drama','showposts'=>-1,'order'=>'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
                <li><a href="<?php echo esc_url(get_category_link(get_cat_ID('Horror')));?>">HORROR</a>
                    <ul>
                        <?php query_posts( array ('post_type'=>'horror','showposts'=>-1,'order'=> 'ASC') ); while ( have_posts() ) : the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; wp_reset_query(); ?>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    <?php
    $products = ob_get_clean(); // store all output from above
    return $items . $products; // add stored output to end of $items and return
}
0
1

Hard code the container instead, remove it like this:

$args = array(
    'theme_location' => 'XXX',
    'container' => false
)

and hard code it around the wp_nav_menu, remembering to add your static section in wherever you need it:

echo '<ul>';
echo '<li><a href="http://...">Static Link</a></li>';
wp_nav_menu($args);
echo '</ul>';

You can use that info to build your nav however you want. Basically, removing the container will make it just spit out a bunch of LI elements with your links in, so put the wp_nav_menu wherever you need them to be and just code around it.

3
  • query_posts function doesn't work in echo command . I tried it but couldn't find a way to run. Next answer is more useful for me.Thanks for your time :)
    – drgky
    Feb 13, 2015 at 15:41
  • but I've not used query_posts in an echo? Are you getting the two answers muddled? ;)
    – efreeman
    Feb 13, 2015 at 16:33
  • How does this cope with sub menus if there is no container? Oct 14, 2016 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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