Conditional Menu in WordPress -
i'm trying show wordpress menu based on value of cookie. in example, i'm using cookies define geographical region user wants view. (i'm still working on part i'm manually defining during development.) based on this, want use either menu1 or menu2. i'm using following code:
function pstv_set_cookie() { $expire=time()+60*60*24*30; setcookie("region", "1", $expire); } add_action( 'init', 'pstv_set_cookie'); if ($_cookie[$region] = "1"){ //use menu 1 wp_nav_menu( array('menu' => 'menu1' )); //wp_nav_menu( array( 'theme_location' => 'menu1' ) ); }elseif ($_cookie[$region] = "2"){ //use menu 2 wp_nav_menu( array('menu' => 'menu2' )); //wp_nav_menu( array( 'theme_location' => 'menu2' ) ); }
this works expected, spits out menu html before else.
where add coded hook wordpress menu?
note: i've tried:
wp_nav_menu( array('menu' => 'menu2' )); , wp_nav_menu( array( 'theme_location' => 'menu2' ) );
(i'm not sure of difference.) in advance
wp_nav_menu displays navigation menu @ spot, using init hook, runs before else, figured.
either:
- run code within template between html
- use filter wp_nav_menu (it has called somewhere)
example filter (not tested):
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args_filter'); function my_wp_nav_menu_args_filter($args = array()) { $args['menu'] = 'menu1'; return $args; }
Comments
Post a Comment