Changing Wordpress PHP to Show Different Menu to Logged In Users - php

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

Related

Why wp_nav_menu does not retrieve menu in correct order?

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',
) );

Setting Wordpress Primary Menu Programmatically

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
}
?>

Wordpress menus

i m having a problem with menus.
I had a template that wasnt originaly for WP.
so i integrated it.
when i started the site there was only one menu which works great.
i added <?php wp_nav_menu('primary'); ?> and its all ok
Now i need to add another one (custom links in the header section)
sooo i used this method
register_nav_menus( array(
'header' => 'Header menu',
'footer' => 'Footer menu'
) );
and recieved new locations for menus.
for the second menu i added
<?php wp_nav_menu( array( 'theme_location' => 'header', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
now the problem:
there is a primary menu (it has few items)
and the custom menu ( its currently empty)
when i add new page to custom menu its being shown in both primary and custom menus.
if i delete all the items from custom menu, primary menu items are back in place...
what is the problem?
Thank you.
If I understand correctly, it looks like you are pointing to the wrong registered menu in the wp_nav_menu(). Specifically in the theme_location.
When you are building the "second menu" which is the "footer", theme_location should be footer since that is the name you gave it in the register_nav_menus().
<?php wp_nav_menu(
array( 'theme_location' => 'footer', // Change 'header' to 'footer'
'menu_class' => 'nav-menu',
'fallback_cb' => false )
);
?>

Create own footer menu in Wordpress?

I do have two menus in my Wordpress-Page which are called
Mainmenu(Primary Menu)
Footer
I associated the "Mainmenu" to the header of the website. I want the footer of the Wordpresspage to look exactly like the header, only difference should be the Links in it. It should not be taken from the "Mainmenu", but from the menu called "Footer". The function of the Header looks like this:
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
rtp_hook_begin_primary_menu();
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) && has_nav_menu( 'primary' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'primary', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
rtp_hook_end_primary_menu();
echo '</nav>';
The Theme i am using is rtPanel. Can anyone tell me how i simply can change this function in the way that it will load its content from the Menu called "Footer" and not from the Primary Menu which is "Mainmenu"?
Please dont worry about the CSS and everything, i will take care about this. I really just want to now how i have to change this function to load the "Footer"-Menu in it and not the Primary-Menu(Mainmenu).
As an Attachment how it look in the menu:
Thank you very much advance!
This is really simple, in fact when you start developing wordpress theme this might be one of the very first items you need to learn.
You need to register a menu location for footer seciton. You can learn more in codex
register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
This will register the menu for you. Since you already have the menu location just find the location parameter. We're going to use that in the following code. For now assume it 'footer' like the above one. And the code for the footer menu according to your referenced code-
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
//rtp_hook_begin_primary_menu(); // we better disable this hook as it intends for the header primary menu
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'footer', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
//rtp_hook_end_primary_menu(); // we better disable this hook as it intends for the header primary menu
echo '</nav>';
Notice the theme_location parameter in wp_nav_menu() function? This is the one which plays behind to get you menu correctly.
Now play around with CSS and you might get what you intend.
Hope this might help you.

Need to customize the top menu in woothemes canvas, depending if the user is logged in or not I need to show 1 menu instead of the other

I've already created a canvas child-theme. I added my custom page woo-hooks.class.php in the canvas-child folder in which i copied all the code from the original and added here:
$this->hooks['header'] = array(
'woo_top' => array(
'content' => '',
'shortcodes' => 0,
'php' =>'
the following code:
if ( is_user_logged_in() ) {
wp_nav_menu( array( "theme_location" => "top-menu-loggedin" ) );
} else {
wp_nav_menu( array( "theme_location" => "top-menu-loggedout" ) );
}
the code is like it doesn't even exists, I know I've probably mistaken what I have to do, but i couldn't find a clear guide anywhere on how to do this, just some code in some forums with no context about how and where to act (the code i added I found it in a forum, but I guess i didn't add it in the right place). The filter and hooks guides of WP don't make clear WHERE and HOW to call the custom filters and hooks and how to actually make them interact with the existing code... I'm going nuts, please help.
Top menu customization can be performed in header section, that is in child theme's 'header.php' file or in other file, specifying 'theme-location' parameter.
You can write below code, in your child theme,
if (is_user_logged_in()){
wp_nav_menu(array(
'menu_class' => 'member-menu',
'menu' => 'Members',
'theme_location' => 'primary');
);
}else{
wp_nav_menu(array(
'menu_class' => 'top-menu',
'menu' => 'Members non Logged in',
'theme_location' => 'primary')
);
}
Here, 'theme-location' parameter is specifies location of Menu and 'menu' specifies name of Menu.

Categories