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 🙂
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 );
}
?>
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 );
}
Sure. I get the following on my homepage:
int(1)
string(0) “”
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?
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.
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 );
}
?>
Fixed…You’re Awesome!! Thanks so much for your help!
You’re absolutely welcome. Sorry for the confusing posts 🙂