Conditional Menus


I managed to get a child theme for twentyten working with conditional menus based on the following article. Basically, if you’re logged in as a member, it will replace the standard menu with a secondary “members” menu.

(N.B. Apart from the instructions in the link, the only other change I had to make, was in menus, I had to tick the members tick box (new tick box appeared after code change) against the members menu and also tick primary against the primary menu, otherwise both menus were concatenated.)

As per the article, I added the following code to my twentyten-child/function.php and it worked fine, so I did the same for travelify-child/function.php and I believe that should work.

	if (!function_exists('wp_nav_menu')) {
	    function wp_nav_menu() {
	    register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'travelify-child' ),
		'members' => __( 'Members Only Navigation', 'travelify-child' ),
	) ); } }

## 2 Problems ##

Problem 1) The code that controls the menu for the Travelify theme isn’t in the same .php file as the twentyten theme, it’s in /public_html/wp-content/themes/travelify/library/structure/header-extensions.php and not in /public_html/wp-content/themes/travelify/header.php. Unfortunately header-extensions.php is ignored by child themes, as it’s a non standard WordPress php file.

So I ran some tests and moving the code below from /public_html/wp-content/themes/travelify/library/structure/header-extensions.php and putting it in /public_html/wp-content/themes/travelify-child/header.php seems to work and puts the menu in the right place, assuming I add it just before the </header> tag. If I upgrade the theme I know header-extensions.php will be overwritten in the parent theme and it will “add back in” the block of code I’ve taken out, so is there a another solution apart from continually removing code from /public_html/wp-content/themes/travelify/library/structure/header-extensions.php after each theme update?

Is there anything I can add to travelify-child/function.php to remove the code from /public_html/wp-content/themes/travelify/library/structure/header-extensions.php?

		if ( has_nav_menu( 'primary' ) ) {
			$args = array(
				'theme_location'    => 'primary',
				'container'         => '',
				'items_wrap'        => '<ul class="root">%3$s</ul>'
			echo '<nav id="main-nav" class="clearfix">
					<div class="container clearfix">';
				wp_nav_menu( $args );
			echo '</div><!-- .container -->
					</nav><!-- #main-nav -->';
		else {
			echo '<nav id="main-nav" class="clearfix">
					<div class="container clearfix">';
				wp_page_menu( array( 'menu_class'  => 'root' ) );
			echo '</div><!-- .container -->
					</nav><!-- #main-nav -->';

Problem 2) I can’t replace the code as it isn’t “drop-in” replacement code. So how do I integrate the below “if statement” for the is_user_logged_in() function in to the above code?

//if the user is logged in, members only menu appears, if not, global menu appears
if ( <strong>is_user_logged_in()</strong> ) :?>
<?php wp_nav_menu
     ( array( 'container_class' => 'menu-header', 'theme_location' => 'members' ) ); ?>
<?php else :?>
<?php wp_nav_menu
     ( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
<?php endif;?>

Whilst I can tinker with other people’s code, I’m not a developer, so if you could demonstrate your point with code, it would be appreciated!

Problem 2 is unfortunately a “show stopper” for me, so I hope you can help.



I found an alternate free plugin solution: