Support » Themes and Templates » 'fallback_cb' => false.. Not Working!

  • Resolved sbaroulette

    (@sbaroulette)


    Hi! I’ve created custom menus in my sidebar which get called dynamically based on a field value in the page. However, I don’t want to see any menu on those pages without the field set. My homepage is showing my main menu although I have a fallback set to false. Please tell me what I’m missing.

    <?php wp_nav_menu( array( 'fallback_cb' => false , 'container' => 'div', 'container_class' => ' widget_wrap widget_nav_menu', 'theme_location' => 'primary', 'menu' => get_post_meta( $post->ID, 'MenuName', true) ) ); ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • jeffpurcell

    (@jeffpurcell)

    You might try something like this:

    <?php
    	$menu_args = array(
    		'fallback_cb' => false,
    		'container' => 'div',
    		'container_class' => ' widget_wrap widget_nav_menu',
    		'theme_location' => 'primary',
    		'menu' => get_post_meta( $post->ID, 'MenuName', true)
    	);
    
    	if( isset( $field_value ) && ! empty( $field_value ) ) {
    		wp_nav_menu( $menu_args );
    	}
    
    ?>

    Obviously you’ll need to change $field_value to the proper variable name.

    Let me know if that works 🙂

    sbaroulette

    (@sbaroulette)

    No luck.. I still see the main menu on the homepage on all other pages without custom field. Is my code/syntax correct?

    <?php  $menu_args = array(
    		'fallback_cb' => false,
    		'container' => 'div',
    		'container_class' => ' widget_wrap widget_nav_menu',
    		'theme_location' => 'primary',
    		'menu' => get_post_meta( $post->ID, 'MenuName', true)
    	);
     	$field_value = 'MenuName';
    	if( isset( $field_value ) && ! empty( $field_value ) ) {
    		wp_nav_menu( $menu_args );
    	}
    ?>

    jeffpurcell

    (@jeffpurcell)

    Can you run this code and then paste the output of the two var_dump() calls?

    echo '<pre>';
    var_dump( $post-ID );
    var_dump( get_post_meta( $post->ID, 'MenuName', true) )
    echo '</pre>';
    
    $menu_args = array(
            'fallback_cb' => false,
            'container' => 'div',
            'container_class' => ' widget_wrap widget_nav_menu',
            'theme_location' => 'primary',
            'menu' => get_post_meta( $post->ID, 'MenuName', true)
    );
    
    $field_value = 'MenuName';
    
    if( isset( $field_value ) && ! empty( $field_value ) ) {
            wp_nav_menu( $menu_args );
    }
    sbaroulette

    (@sbaroulette)

    Sure. I get the following on my homepage:
    int(1)
    string(0) “”

    jeffpurcell

    (@jeffpurcell)

    Ah ha! There’s the problem. Your custom field is empty. In the menu args array can you the get_post_meta() with the value you are expecting to be returned and then post your findings?

    sbaroulette

    (@sbaroulette)

    Sorry I’m confused… I thought I wanted it to be empty so that the fallback will kick in.. I am not expecting a value on the homepage bc I have no custom menu for it.

    jeffpurcell

    (@jeffpurcell)

    You’re right. I got confused for a moment about what the end goal was. I apologize 🙂

    Yes, you do want the get_post_meta() to return an empty string on the home page. I think a quick restructuring of the code will fix all of this:

    <?php  
    
    	$field_name = get_post_meta( $post->ID, 'MenuName', true);
    
    	# $field_name will be an empty string
    	# if the custom field doesn't exist
    	# for this page.
    
    	if( '' != $field_name ) {
    
    		$menu_args = array(
    			'fallback_cb' => false,
    			'container' => 'div',
    			'container_class' => ' widget_wrap widget_nav_menu',
    			'theme_location' => 'primary',
    			'menu' => $field_name
    		);
    
    		wp_nav_menu( $menu_args );
    	}
    ?>
    sbaroulette

    (@sbaroulette)

    Fixed…You’re Awesome!! Thanks so much for your help!

    jeffpurcell

    (@jeffpurcell)

    You’re absolutely welcome. Sorry for the confusing posts 🙂

    Resolved!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘'fallback_cb' => false.. Not Working!’ is closed to new replies.