• Resolved DefJam

    (@defjam)


    Hi,
    I’d like to display different main navigation menu on certain categories/posts. Went through this topic:
    /support/topic/custom-menus-on-different-pages
    I get the idea, and it is very end-user friendly. I’m implementing it through Advanced Custom Fields by using a checkbox:
    [*]Do you want the alternative menu?
    If checked, the field MenuType gets a value of alt_main_nav.
    I’ve registered a new menu location, alt_main_nav.
    I’m using Bones, so my main nav functions is:

    function bones_main_nav() {
        wp_nav_menu(
        	array(
        		'menu' => 'main_nav', /* menu name */
        		'menu_class' => 'nav',
        		'theme_location' => 'main_nav', /* where in the theme it's assigned */
        		'container' => 'false', /* container class */
        		'fallback_cb' => 'bones_main_nav_fallback', /* menu fallback */
        		'depth' => '3', /* suppress lower levels for now */
        		'walker' => new Bootstrap_Walker()
        	)
        );
    }

    In my header.php I got:

    <div class="nav-collapse">
    	<?php bones_main_nav (
    		wp_nav_menu(
    			array(
    				'menu' => get_field('MenuType'),
    				'menu_class' => 'nav',
    	    		'theme_location' => 'main_nav', /* where in the theme it's assigned */
    	    		'container' => 'false', /* container class */
    	    		'fallback_cb' => 'bones_main_nav_fallback', /* menu fallback */
    	    		'depth' => '3', /* suppress lower levels for now */
    	    		'walker' => new Bootstrap_Walker()
    				)
    			)
    	); // Adjust using Menus in WordPress Admin ?>
    </div>

    And the result is that I get the nav menu twice:
    main_nav
    main_nav
    on “normal” pages/categories/posts and only main_nav should be visible and
    alt_main_nav
    main_nav
    on posts where the custom field is set and only alt_main_nav should be visible.

    I’m a total newbie on PHP so I believe I’m not replacing the ‘menu’ correctly in the function above.
    Please advice,
    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I don’t know much about the bones theme and its framework. If I understand correct you want to display a default menu on all pages and only the alt_main_nav on posts/pages with a “custom field”

    First create a Child Theme else all the changes will be overwritten when this theme is updated.

    Copy the header.php file from the main theme to the child theme wp-contents/themes/bones-child/header.php. Create (don’t copy) a functions.php file in this directory bones-child and place the following code.

    <?php
    register_nav_menu( 'alt_main_nav', 'Alternate Main Menu' );

    The closing PHP tag ?> has been deliberately omitted. Save the file and edit the wp-contents/themes/bones-child/header.php file.

    Go to line 58, remove the existing <?php bones_main_nav(); ?> and place this code

    <?php
    if( empty( get_post_meta( get_the_ID(), 'alt_main_nav', true ) ) ) :
    	bones_main_nav();
    else :
    wp_nav_menu( array(
    	'container' => false,
    	'container_class' => 'menu clearfix',
    	'menu_class' => 'nav top-nav clearfix',
    	'theme_location' => 'alt_main_nav',
    	'before' => '',
    	'after' => '',
    	'link_before' => '',
    	'link_after' => '',
    	'depth' => 0,
    	'fallback_cb' => 'bones_main_nav_fallback'
    	)
    );
    endif;
    ?>

    Save the file and go to Appearance > Menus. Create a new menu and go to the Locations tab. Choose this newly created menu for the “Alternate Main Menu” location.

    Edit the posts/pages for which you want this Alternate menu to be displayed. Scroll down to Custom Fields and enter “alt_main_nav” for the Name and “true” for the value.

    Thread Starter DefJam

    (@defjam)

    … and Thank you!
    This works perfectly and as expected!

    Great to hear that! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Bones] Different navigation menu on certain categoy/post’ is closed to new replies.