WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 'fallback_cb' => false.. Not Working! (11 posts)

  1. sbaroulette
    Member
    Posted 1 year ago #

    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) ) ); ?>

  2. jeffpurcell
    Member
    Posted 1 year ago #

    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 :)

  3. sbaroulette
    Member
    Posted 1 year ago #

    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 );
    	}
    ?>
  4. jeffpurcell
    Member
    Posted 1 year ago #

    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 );
    }
  5. sbaroulette
    Member
    Posted 1 year ago #

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

  6. jeffpurcell
    Member
    Posted 1 year ago #

    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?

  7. sbaroulette
    Member
    Posted 1 year ago #

    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.

  8. jeffpurcell
    Member
    Posted 1 year ago #

    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 );
    	}
    ?>
  9. sbaroulette
    Member
    Posted 1 year ago #

    Fixed...You're Awesome!! Thanks so much for your help!

  10. jeffpurcell
    Member
    Posted 1 year ago #

    You're absolutely welcome. Sorry for the confusing posts :)

  11. sbaroulette
    Member
    Posted 1 year ago #

    Resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic