• Resolved djeyewater

    (@djeyewater)


    I have created a child theme of the 2012 theme, and in the child theme I have created a new page template. In this template I want to add a menu to the sidebar (so the menu will only appear on pages using this particular template).

    I have had a look at the wp_nav_menu function, but it seems like this needs the nav_location to be set within the sidebar, and I am not sure how I would do that.

    I don’t want to use a custom sidebar (unless this can be made to inherit all widgets set on the main sidebar)?

Viewing 9 replies - 1 through 9 (of 9 total)
  • You would need to register a new menu location in your child theme’s functions.php file and then call wp_nav_menu() using the new menu location in your custom template file.

    Thread Starter djeyewater

    (@djeyewater)

    Okay, but how do I register a new menu location within the sidebar?

    Edit: I think I’ve got it now, in my page template I removed
    <?php get_sidebar();?>
    and replaced it with

    <div id="secondary" class="widget-area" role="complementary">
    			<?php dynamic_sidebar( 'sidebar-1' ); ?>
    			<?php wp_nav_menu(array('theme-location' => 'extra', 'menu' => 'extra-menu')); ?>
    		</div><!-- #secondary -->

    I’m guessing that’s the correct way to do it? (Seems to work anyway)

    See the page I linked to above.

    Thread Starter djeyewater

    (@djeyewater)

    Thanks, I understand that register_nav_menu can be used to create a named location for a nav menu. But that page doesn’t seem to specify how you would make it so that the named location then correlates to a position inside the sidebar.

    The bit I was confused with was how to make that named location be inside the sidebar. But after reading the docs for get_sidebar, and seeing that all it was doing was a simple include, I realised that I could just copy across the contents of sidebar,php into my page template and then modify it as necessary. (As per my previous post).

    Hi djeyewater

    Could you do a run down as in go through what you did to get it to work?
    (a short tutorial)

    I have myself added a sidebar menu theme location in the functions file. But now I can uncertain where to add the code to get the sidebar visible.
    Do I place it in the page.php, single.php and index.php? And if so what would the code be?

    Thanks.

    Thread Starter djeyewater

    (@djeyewater)

    Hi Paal

    I can’t remember the exact details now, but I’ll look into it and do a short tutorial like you requested. I’ll post back here when it’s done.

    Dave

    Great!

    This is what I have done. I am using a one sidebar theme and have through the functions file added another sidebar. The 2 sidebar is located on the right. The first on the left.
    I am then looking into showing the new sidebar theme location in the left sidebar.
    To make it even more tricky. The theme can either have the first sidebar on the left or right….

    Thread Starter djeyewater

    (@djeyewater)

    Hi Paal

    I’ve found there is already a good tutorial on adding another sidebar to your theme: https://www.tastyplacement.com/add-sidebar-wordpress

    But to go through it quickly, in your functions.php you register your new sidebar.

    register_sidebar(array(
    	'name'          => __( 'Sidebar two', 'twentytwelve' ),
    	'id'            => 'sidebarTwo',
    	'description'   => 'Sidebar No. 2',
            'class'         => '',
    	'before_widget' => '<li id="%1$s" class="widget %2$s">',
    	'after_widget'  => '</li>',
    	'before_title'  => '<h2 class="widgettitle">',
    	'after_title'   => '</h2>' ));

    In your theme folder create a new file that will contain the code for your new sidebar. It should be named sidebar-whatever.php. In this file you need to call the sidebar you registered, ‘Sidebar two’ in my example. And if you are adding a menu placement in this sidebar, you need to set the menu location using wp_nav_menu. So, my file ‘sidebar-2.php’ contains:

    <div id="sidebar2Container" class="widget-area" role="complementary">
       <ul>
          <?php dynamic_sidebar('Sidebar two');?>
          <?php wp_nav_menu( array( 'theme_location' => 'sidebar two' ) );?>
       </ul>
    </div>

    At the moment we have placed where our custom menu will show up, but we haven’t actually made that placement available to choose in the admin area. To do, in your Functions.php file add:

    register_nav_menu( 'sidebar two', 'Sidebar 2' );

    Now when you create a menu in the admin area, you should see ‘Sidebar 2’ listed under the theme locations where the menu can appear.

    Finally, we need to make it so that our sidebar will actually appear. To do this, edit the page template(s) that you want to include the new sidebar. For this example, I’m going to add the new sidebar only to single pages, so I’m just going to edit single.php.

    At the bottom of single.php I added the call to my new sidebar. The last 3 lines of my single.php look like this:

    <?php get_sidebar(); //get the default sidebar ?>
    <?php get_sidebar('2'); //get our sidebar 2 (sidebar-2.php) ?>
    <?php get_footer(); ?>

    As you can see, my sidebar will be included straight after the primary sidebar. You can, of course, place the call to the sidebar before the content, or wherever you want. Note that I call get_sidebar('2') as my sidebar file is named sidebar-2.php. If my sidebar file was named sidebar-my-super-sidebar.php, then I would need to call get_sidebar('my-super-sidebar') to have it included.

    And that’s it really. For my own purposes that I originally asked this question about, I did something quite different. There are often multiple different ways to get the same thing done. But I believe the above process should probably suit most people wanting to add a second sidebar with a menu location.

    Hopefully someone will correct me if any of the above is wrong. (It works for me but there could be some possible conflict or best practice way of doing things that I have missed).

    Dave

    Thank you very much for sharing Dave!

    I have done something similar.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add a menu in the sidebar in a page template’ is closed to new replies.