Forums

Assigning secondary menu to one page (8 posts)

  1. soulscion
    Member
    Posted 1 year ago #

    Hi,

    I am currently helping to build a site here: http://preview.screencult.com

    I have created a secondary menu by inputting this into functions.php:

    // This theme uses wp_nav_menu() in two locations.   
    
    	register_nav_menus( array(
      		'primary' => __( 'Primary Navigation', 'twentyten' ),
      		'secondary' => __('Secondary Navigation', 'twentyten')
    	) );

    However, I just want the secondary menu (without the primary menu) to appear on one particular page on the site. Where do I place the following code and how to add necessary conditional tags to make sure it only appears on one page?

    <?php wp_nav_menu( array( 'theme_location' => 'secondary' ) ); ?>

    Many thanks, I tried searching for the answer already so my bad if it exists somewhere else.

  2. David Gwyer
    Member
    Posted 1 year ago #

    You can use the conditional tag is_page() to check when you are on a particular page (by entering the page ID as a parameter). See here for more details:

    http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

    The code you need will be something like this:

    if ( is_page('42') )
    {
      if ( has_nav_menu( 'secondary' ) )
      { wp_nav_menu( array( 'theme_location' => 'secondary' ) ); }
    }

    Here '42' is the page id that the menu will appear on ONLY, other pages no menu will be displayed. Also, the has_nav_menu will check that the 'secondary' menu exists.

    Just add the code to your template file wherever you need it (i.e. header.php or elsewhere), or better still add it as a function in functions.php and reference it from your theme files.

  3. soulscion
    Member
    Posted 1 year ago #

    Awesome thank you. Sorry to be rather obvious but how would I go about putting it into functions.php?

    Also having read the link you provided would it work if I inputted:

    if ( is_page('not_yet') )
    {
      if ( has_nav_menu( 'secondary' ) )
      { wp_nav_menu( array( 'theme_location' => 'secondary' ) ); }
    }

    for the page I want (the slug for the page is 'not_yet'). Or is it best to use page id because it comes up as a ?post=853 instead of ?page=853

    Many thanks.

  4. David Gwyer
    Member
    Posted 1 year ago #

    If it comes up as post_id then you want the menu to show on a particular post not page? If so you will need to use is_single() not is_page(). So be clear whether it is a WordPress post or page you wish to display the secondary menu on.

    To embed in the functions.php file simply add the code to a function, for example:

    function show_menu() {
      if ( is_page('not_yet') )
      {
        if ( has_nav_menu( 'secondary' ) )
          { wp_nav_menu( array( 'theme_location' => 'secondary' ) ); }
      }
    }

    Then just add the show_menu() function to the functions.php file and you should be good to go!

  5. soulscion
    Member
    Posted 1 year ago #

    Ok awesome stuff again thanks so much but I do want it to apply to a page. However when I access that page in WP admin to find it's id, the url shows: http://preview.screencult.com/wp-admin/post.php?post=853&action=edit

    So I am a little perplexed there. Any ideas why that is, or is it safe to have is_page('853') or am I missing something blatant?

    Many thanks :D

  6. David Gwyer
    Member
    Posted 1 year ago #

    Should be fine to use '853' or the slug, as the is_page function will take both. Best way is to try it and see!

  7. bohica032
    Member
    Posted 9 months ago #

    So as a total noob... I can't get my head wrapped around it.

    So this is what I have so far and can't get my 2nd menu to appear.

    File: functions.php

    if (function_exists('register_nav_menus')) {
    		register_nav_menus(
    			array(
    				'main_nav' => 'Main Navigation Menu',
    				'product_nav' => 'Product Menu'
    			)
    		);
    	}
    
    	function show_menu() {
    	  if ( is_page('products') )
    	  {
    	    if ( has_nav_menu( 'product_nav' ) )
    	      { wp_nav_menu( array( 'theme_location' => 'product_nav' ) ); }
    	  }
    	}

    What do I enter in my page.php to display Product Menu? I want the 2nd menu to be visible only on product page only.

    Please help

  8. jedeso
    Member
    Posted 8 months ago #

    help....can someone pls. tell me the specific code to use for moving the secondary menu over to the right? i am using wordpress v 3.2.1 with weaver theme 2.2.4

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags