This topic has 19 replies, 5 voices, and was last updated 8 years by clubmasterminds.
-
Author
-
August 27, 2014 at 22:53 #26871claremcsharryParticipant
I just bought the Kleo theme and love it.
I would like to display a top menu and a main menu when a user is logged in.
When a user is logged out I want to display a menu with just two options.I have created 3 menus –
main_menu
top-menu
logged-outI want logged out menu to be displayed when the user is logged out and
top-menu and main_menu to be displayed when the user is logged in.I added the following code to the functions.php file on the child theme:
function my_wp_nav_menu_args( $args = ” )
{
if( is_user_logged_in() ) {
$args[‘menu’] = ‘menu_main’;
}
else {
$args[‘menu’] = ‘logged-out’;
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );My settings in wordpress are as per attachment one
With these settings, the menu called logged-out shows when the user is logged out which is fine but only menu_main is shown in both the top menu and the main menu.
I have googled this problem and I have seen an alternative piece of code that works for the twenty twenty theme:
Code For twenty twenty theme:
<?php
if ( wp_emember_is_member_logged_in() ) {
wp_nav_menu( array( ‘menu’ => ‘logged-in-members’, ‘container_class’ => ‘menu-header’ ));
}
else {
wp_nav_menu( array( ‘menu’ => ‘normal-visitor-menu’, ‘container_class’ => ‘menu-header’ ));
}
?>
They get the container values from the header.php file. The code is put into the header.php file.
Is there anything similar I can do to some similar php code , so that I use my three different menus in the Kleo theme?
Regards,
Clare
Attachments:
You must be logged in to view attached files.August 28, 2014 at 00:57 #26876sharmstrModeratorIf it were me, I’d take a look at kleo/page-parts/general-header-section.php.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
August 28, 2014 at 01:59 #26884claremcsharryParticipantThank you – that looks like the appropriate program.I will try changing it.
August 28, 2014 at 03:20 #26894claremcsharryParticipantHi there,
I copied that code – kleo/page-parts/general-header-section.php. into the child theme – kleo-child/page-parts/general-header-section.php:
I changed the following code:
$top_menu = wp_nav_menu( array(
‘theme_location’ => ‘top’,
‘depth’ => 2,
‘container’ => ‘div’,
‘container_class’ => ‘top-menu col-sm-12 col-md-7 no-padd’,
‘menu_class’ => ”,
‘fallback_cb’ => ”,
‘walker’ => new kleo_walker_nav_menu(),
‘echo’ => false
)
);$primary_menu = wp_nav_menu( array(
‘theme_location’ => ‘primary’,
‘depth’ => 3,
‘container’ => ‘div’,
‘container_class’ => ‘collapse navbar-collapse nav-collapse pull-right’,
‘menu_class’ => ‘nav navbar-nav’,
‘fallback_cb’ => ‘kleo_walker_nav_menu::fallback’,
‘walker’ => new kleo_walker_nav_menu(),
‘echo’ => false
)
);To
if ( is_user_logged_in() ) {
$top_menu = wp_nav_menu( array(
‘theme_location’ => ‘top’,
‘depth’ => 2,
‘container’ => ‘div’,
‘container_class’ => ‘top-menu col-sm-12 col-md-7 no-padd’,
‘menu_class’ => ”,
‘fallback_cb’ => ”,
‘walker’ => new kleo_walker_nav_menu(),
‘echo’ => false,
‘menu’ => ‘Top-menu’
)
);$primary_menu = wp_nav_menu( array(
‘theme_location’ => ‘primary’,
‘depth’ => 3,
‘container’ => ‘div’,
‘container_class’ => ‘collapse navbar-collapse nav-collapse pull-right’,
‘menu_class’ => ‘nav navbar-nav’,
‘fallback_cb’ => ‘kleo_walker_nav_menu::fallback’,
‘walker’ => new kleo_walker_nav_menu(),
‘echo’ => false,
‘menu’ => ‘menu_main’
)
);
}
else {
$primary_menu = wp_nav_menu( array(
‘theme_location’ => ‘primary’,
‘depth’ => 3,
‘container’ => ‘div’,
‘container_class’ => ‘collapse navbar-collapse nav-collapse pull-right’,
‘menu_class’ => ‘nav navbar-nav’,
‘fallback_cb’ => ‘kleo_walker_nav_menu::fallback’,
‘walker’ => new kleo_walker_nav_menu(),
‘echo’ => false,
‘menu’ => ‘logged_out’
)
);
}Now the menu logged_out is displayed as the main menu and there is no top menu when the user is logged out. This is perfect.
However when the user logs in, the menu main_menu is displayed on the top menu and main menu. There is no sign of the top-menu I created.On my settings on the backend, I have set main menu to logged_out and top menu to blank.
Can anyone help – should I be using a referencing a different value than menu in the code?
August 28, 2014 at 06:07 #26912sharmstrModeratorHonestly, that’s exactly what I would have done. If I have time during morning coffee I’ll see if I can figure it out. Let me know if you figure it out before then.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
August 28, 2014 at 12:36 #26937sharmstrModeratorI copied your code and it worked great. Double check your menu names in your code. I noticed that you used an underscore for one name and a hyphen for the other so maybe one is a typo? If wordpress cant find the menu, it will display one that it can find.
See attached. Again, I just used your code and changed the names of my test menus.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
Attachments:
You must be logged in to view attached files.August 28, 2014 at 12:37 #26939sharmstrModeratorHere’s the logged out screen shot.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
Attachments:
You must be logged in to view attached files.August 28, 2014 at 15:14 #26956claremcsharryParticipantI did indeed have the top menu spelled wrong. I changed it to Top_menu but no luck, it still won’t work.
I am running WordPress on my local machine – could this make a difference?
To login I am using a shortcode from a plug-in called UPme profiles – could this mess things up?
How are you logging in?August 28, 2014 at 16:16 #26972sharmstrModeratorI have no idea about that plugin. Have you tried disabling it and testing the menu code? I’m logging in with the standard wp login.
I doubt this makes a difference, but try changing your menu locations. Set Primary to menu_main and Top Menu to Top_menu. Also, not sure if it matters, but is Top_menu actually capitalized? (I’m really taking shots in the dark here, lol)
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
Attachments:
You must be logged in to view attached files.August 28, 2014 at 21:29 #27043claremcsharryParticipantHi there,
Thanks again for your suggestions.
I had a copy of the website that I created using Duplicator. I have been using that version to try out various plug-ins.
I just copied the code into it and the login and logout all works!It’s weird. I think the fact that I spelled the top menu wrong (spelled it top-menu instead of Top_menu) must have messed things up. Even though I corrected it, it didn’t seem to “see” it.
Could it have been sitting in some cache memory?
Copying the correct code into the other version of the site works anyway.Thanks so much for all your help. Only for you said the code worked I wouldn’t have kept at it. It’s so more satisfying sorting it out by changing some code than installing yet another plug-in.
Regards,
Clare.August 28, 2014 at 21:56 #27046sharmstrModeratorAwesome. Glad you sorted it out.
btw – I’m sure you’ll be posting code here again, so in future wrap the code with a < pre > tags.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
August 29, 2014 at 15:34 #27114propulsaParticipantI just read the whole topic because I would be also interested on this and I tried copying that code changing the names of the menus on my general-header-section.php but when I do it the website doesn’t work for a sintax error on the code line. There is something else to do besides that code on that php page?
Thank you.
August 29, 2014 at 16:38 #27123sharmstrModeratorFix your single quotes. When you post code on this forum and dont use the pre or code tags, it will change single quotes to fancy quotes.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
August 29, 2014 at 16:42 #27124sharmstrModeratorHere’s the fixed quotes
COPY CODEif ( is_user_logged_in() ) { $top_menu = wp_nav_menu( array( 'theme_location' => 'top', 'depth' => 2, 'container' => 'div', 'container_class' => 'top-menu col-sm-12 col-md-7 no-padd', 'menu_class' => ”, 'fallback_cb' => ”, 'walker' => new kleo_walker_nav_menu(), 'echo' => false, 'menu' => 'Top-menu' ) ); $primary_menu = wp_nav_menu( array( 'theme_location' => 'primary', 'depth' => 3, 'container' => 'div', 'container_class' => 'collapse navbar-collapse nav-collapse pull-right', 'menu_class' => 'nav navbar-nav', 'fallback_cb' => 'kleo_walker_nav_menu::fallback', 'walker' => new kleo_walker_nav_menu(), 'echo' => false, 'menu' => 'menu_main' ) ); } else { $primary_menu = wp_nav_menu( array( 'theme_location' => 'primary', 'depth' => 3, 'container' => 'div', 'container_class' => 'collapse navbar-collapse nav-collapse pull-right', 'menu_class' => 'nav navbar-nav', 'fallback_cb' => 'kleo_walker_nav_menu::fallback', 'walker' => new kleo_walker_nav_menu(), 'echo' => false, 'menu' => 'logged_out' ) ); }
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
September 1, 2014 at 12:25 #27221propulsaParticipantI am still having problems with the code I wrote and the web doesn’t load for a syntax error when we specify the name of the menu like doesn’t understand that line.
Parse error: syntax error, unexpected ”menu” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in /home/propulte/public_html/mcdf/wp-content/themes/kleo/page-parts/general-header-section.php on line 29
September 1, 2014 at 14:31 #27230sharmstrModeratorCan paste your code here? Please wrap it in pre tags.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
September 1, 2014 at 14:49 #27236propulsaParticipantOk. Got it. Finally I made it works. Thank you.
September 16, 2014 at 15:09 #28748potion5ParticipantA slightly different approach: register a second, alternate menu for each menu theme location. Then show the alternate menu conditionally on logged-in status. Put it in a plugin and it will work for any theme.
COPY CODE<?php /* Plugin Name: Potion Conditional Menus Description: Show alternate menus for logged in users. This plugin will create duplicate theme menu locations; for example, "Primary Menu" will get a counterpart called "Primary Menu for logged in users". Author: Denis Collis - Potion Design & Digital Version: 1.0 Author URI: http://www.potion.co.za */ /* Register Alternate Menus */ add_action( 'init', 'register_alt_menus', 9999 ); function register_alt_menus() { $existing_menus = get_registered_nav_menus(); foreach ($existing_menus as $location => $description){ register_nav_menu( $location . "_logged_in", $description . " for logged in users"); } } /* Show alternate menus if user is logged in */ add_filter('wp_nav_menu_args', 'show_alt_menu'); function show_alt_menu($args){ if ( is_user_logged_in() ) { $args['theme_location'] = $args['theme_location'] . '_logged_in'; } return $args; }
March 2, 2016 at 23:41 #108136clubmastermindsParticipantI appreciate the text you have provided to do this but can you let me know what part to change? I can’t figure out what the menu name change is to change out my menu name.
Thank you
-
AuthorPosts
The forum ‘KLEO’ is closed to new topics and replies.