I currently have two menus created via the back end. I need to activate the menu based on the geo location, which I have aleady setup to set a default currency. The menu names in the backend are Main and Main International. Below is the code:
function geo_client_currency($client_currency){
$userInfo = geoip_detect2_get_info_from_current_ip();
if ($userInfo->country->isoCode == 'US'){
$client_currency = 'USD'; //currency code
}
else {
$client_currency = 'INR';
}
return $client_currency;
}
So essentially what I need to do is set the Main menu for the US and Main International for anywhere outside the US. I have reviewed the Codex but not quite sure how to implement that the easiest way possible
<?php
$userInfo = geoip_detect2_get_info_from_current_ip();
if ( $userInfo->country->isoCode == 'US' ){
wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu ) );
} else {
wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main International' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu ) ); //Change 2 to be the Main International ID
}
?>
Could it be something as easy as this in the template file (like header.php)?
<?php
$userInfo = geoip_detect2_get_info_from_current_ip();
if ( $userInfo->country->isoCode == 'US' ){
wp_nav_menu(array('menu' => 1)); //Change 1 to be the Main ID
} else {
wp_nav_menu(array('menu' => 2)); //Change 2 to be the Main International ID
}
?>
Related
I am trying to pull menu on my header.php using following code:
wp_nav_menu( array(
'theme_location' => 'main-menu',
'menu_id' => 'main-menu',
) );
And in my admin login Appearance > Menu, main-menu is in following order:
Home
About
Tours
Pages
Contact Us
Help Desk
Gallery
Destinations
Blog
Booking
But in header the order is jumbled and even doesn't show the submenu also.
Order on header
About
Pages
Tours
Booking
Home
Blog
Destinations
Help me out! I even tried using order_by/sort_column, they didn't work for me.
I think Below code is what you looking for.
Add your Menu Name Instead of 'Main menu'
<?php
$main_menu = wp_get_nav_menu_items('Main menu');
$customize_arr = array();
if(!empty($main_menu)){
foreach($main_menu as $mm){
$mm = (array) $mm;
if($mm['menu_item_parent'] == 0){
foreach($main_menu as $sm){
$sm = (array) $sm;
if($mm['ID'] == $sm['menu_item_parent']){
(array)$mm['submenu'][] = $sm;
}
}
$customize_array[] = $mm;
}
}
}
?>
In $customize_array you will get all the menus with sub-menu you just have to do foreach loop with your html code.
100% Working code..I hope the Code Will help You!
If you developed the theme from scratch then add the following as an index of your array
'orderby' => 'menu_order'
Would be somthing like below
wp_nav_menu( array(
'theme_location' => 'main-menu',
'orderby' => 'menu_order'
'menu_id' => 'main-menu',
) );
If you are working on an already developed them then the issue is with 'theme_location' index of the array, so change it to 'theme_location' => 'primary' it will be something like below:
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_id' => 'main-menu',
) );
I've spent some time searching, but I haven't really found anything concrete in regards to passing new $args to a navigation widget. I did stumble across this post. However, I think the answer is a little overkill for what I'm trying to achieve.
To sum up the linked post it basically goes on to show how you could accomplish what I need, but only if an entirely new widget is created.
Specifically, I'm looking to either merge or overwrite the following
$args exclusively for a menu widget placed within a Wordpress sidebar;
wp_nav_menu( array $args = array(
'menu' => "header-quicklinks",
'menu_id' => "quicklinks",
'theme_location' => "sidebar-header"
) );
If possible I would like to pass the ID of the widget, in my case nav_menu-6; to the function and have the $args only apply to that menu specifically, this way I can touch up the code to target other menus should I have the requirement.
Currently tinkering with the following;
function widget_nav_args($args){
$menu = $args['menu'];
if($menu->term_id === "menu-quick-links") { // < Error: non-object.
return array_merge( $args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args');
You are nearly there. The widget_nav_menu_args filter accepts more parameters than just the $args for the nav. You want to look at the widget arguments which is the 3rd paremeter. It would look something like this:
function widget_nav_args( $nav_menu_args, $nav_menu, $args, $instance){ // <- notice extra params..
if( $args['id'] === 'sidebarheader' ) { // < This is where we check if it's the right widget
return array_merge( $nav_menu_args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $nav_menu_args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args', 10, 4);
Notice I had to explicitly say how many arguments to pass to my filter function. Be sure to read through the documentation in the WP Codex here.
Hope that helps!
add_filter('widget_nav_menu_args', 'my_wp_nav_menu_args');
function my_wp_nav_menu_args($args) {
if (is_page(2) //Only target page 2
&& $args['theme_location'] === 'primary') { // Check and only target the primary menu
$args['menu'] = 'Menu for Profile';
}
return $args;
}
sample arguments are as below.
$arguments = array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => ''
);
I'm trying to learn some new stuff but I stuck to something easy.
I managed to manipulate a link from wp menu using this code
add_filter( 'nav_menu_link_attributes', 'menu_item_data_toggle', 10, 2 );
function menu_item_data_toggle( $atts, $item) {
// Manipulate attributes
if ( 74 == $item -> ID )
$atts['data-toggle'] = 'modal';
return $atts;
}
but I need to use this modal with "if" attribute for login user and I can't figure how to.
The code for this modal I want to use is from a button
<?php
if( is_user_logged_in() ){
$account_manage = ''.classifieds_get_option( 'my_profile_looks' ).'';
$submit_ad = add_query_arg( array( $classifieds_slugs['subpage'] => 'submit_ad' ), classifieds_get_permalink_by_tpl('page-tpl_my_profile') );
$modal = '';
}
else{
$account_manage = ''.classifieds_get_option( 'login_looks' ).'';
$submit_ad = '#register';
$modal = 'data-toggle="modal"';
}
$locations = get_nav_menu_locations();
if ( isset( $locations[ 'top-navigation' ] ) ) {
wp_nav_menu( array(
'theme_location' => 'top-navigation',
'menu_class' => 'nav navbar-nav clearfix',
'container' => false,
'echo' => true,
'items_wrap' => '<ul class="%2$s">%3$s',
'depth' => 10,
'walker' => new classifieds_walker,
) );
}
if(get_option('users_can_register')){
echo '<li>'.$account_manage.'</li><li class="submit-add">'.esc_html__( 'SUBMIT AD', 'classifieds' ).'</li></ul>';
}
?>
Can you please me give me a hint to figure it out and use that menu link with this modal attributes?
Thank you!
I'm trying to create a simple nav in wordpress, however the wp_nav_menu parameters are ignored , am I missing something obvious?
I'm using a blank template called html5 blank. The Steps i've taken so are listed below
1) Register the menu in functions.php
2) From word press back end Create a menu called 'p' and assign pages
3) Give the menu a theme location
Registering and assigning menu location work fine , some but for some reason the wp_ parameters are ignored EG container , menu class, menu id etc...
If i inspect element, the container 'nav' is missing and the li items have default word press classes
BELOW is my code
Code in header.php
<?php html5blank_nav() ?>
Code in functions.php
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'primary pete',
'menu' => 'p'
'container' => 'nav',
'container_class' => '',
'container_id' => '',
'menu_class' => 'slimmenu',
'menu_id' => 'navigation',
'echo' => true,
'fallback_cb' => 'false',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}
// Register HTML5 Blank Navigation
function register_html5_menu()
{
register_nav_menus(array( // Using array to specify more menus if needed
'primary' => __('primary pete', 'Primary Menu'),
'sidebar-menu' => __('Sidebar Menu', 'html5blank'),
'extra-menu' => __('Extra Menu', 'html5blank')
));
}
// Remove the <div> surrounding the dynamic navigation to cleanup markup
function my_wp_nav_menu_args($args = '')
{
$args['container'] = false;
return $args;
}
// Remove Injected classes, ID's and Page ID's from Navigation <li> items
function my_css_attributes_filter($var)
{
return is_array($var) ? array() : '';
}
// Remove invalid rel attribute values in the categorylist
function remove_category_rel_from_category_list($thelist)
{
return str_replace('rel="category tag"', 'rel="tag"', $thelist);
}
// Add page slug to body class, love this - Credit: Starkers Wordpress Theme
function add_slug_to_body_class($classes)
{
global $post;
if (is_home()) {
$key = array_search('blog', $classes);
if ($key > -1) {
unset($classes[$key]);
}
} elseif (is_page()) {
$classes[] = sanitize_html_class($post->post_name);
} elseif (is_singular()) {
$classes[] = sanitize_html_class($post->post_name);
}
return $classes;
}
Many thanks,
P
When i try to reproduce this the menu with container_class => 'slimmenu' is shown.
Are you sure when you add a class to the container_class parameter this is not shown in the inspector?
Also noticed a missing comma after the parameter 'p' when i copied your code. But i don't think it will resolve your problem.
I'm building a membership website on Wordpress and would like to show a different navigation menu to logged in users.
Here is the current PHP code that displays the menu :
<?php /* Our navigation menu. */ ?>
<?php if ( isset ($options['admired_remove_superfish']) && ($options['admired_remove_superfish']!="") )
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
else
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'menu_class' => 'sf-menu','fallback_cb' => 'admired_page_menu' ) );?>
Here's the PHP code that needs to replace that code :
<?php
if ( wp_emember_is_member_logged_in() ) {
wp_nav_menu( array( 'menu' => 'logged-in-members' ) );
} else {
wp_nav_menu( array( 'menu' => 'normal-visitor-menu' ) );
}
?>
If I just replace the old code, with the newer code it will work, but the formatting is off. I need the Superfish part in the current code, but I'm not sure how to make it work in PHP.
I know this may be a little confusing, but I would appreciate any help. Thanks!
P.S. This is a tutorial from the plugin's site. I've been following it, but I somehow need to keep the Superfish in there. I'm sure not sure how to do it.
http://www.tipsandtricks-hq.com/wordpress-membership/show-different-navigation-menu-to-your-members-and-non-members-551
The 'menu_class' => 'sf-menu' will add the sf-menu class for the menu (<ul class="sf-menu">) and super fish plugin will use this class to identify the menu and style will be applied which has been declared in the super fish plugin's css
<?php
if ( wp_emember_is_member_logged_in() ) {
wp_nav_menu( array( 'menu' => 'logged-in-members', 'menu_class' => 'sf-menu' ) );
} else {
wp_nav_menu( array( 'menu' => 'normal-visitor-menu', 'menu_class' => 'sf-menu' ) );
}
?>
For more see this.
Considering the code above, the only thing that is changing is the actual location of the menu. The (existing) code is showing that you want the menu that's in 'theme_location' => 'primary' where, as you have a hardcoded menu you want to use, and you're selecting it with 'menu' => 'loggged-in-members' The finished result will be...
<?php
if ( wp_emember_is_member_logged_in() ) {
wp_nav_menu(
array(
'container_class' => 'menu-header',
'menu' => 'logged-in-members',
'menu_class' => 'sf-menu',
'fallback_cb' => 'admired_page_menu'
)
);
} else {
wp_nav_menu( array( 'menu' => 'normal-visitor-menu' ) );
}
?>