This topic has 19 replies, 5 voices, and was last updated 8 years by clubmasterminds.

  • Author

    Tagged: , , , ,

  • #26871
     claremcsharry
    Participant

    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-out

    I 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.
    #26876
     sharmstr
    Moderator

    If 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 solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #26884
     claremcsharry
    Participant

    Thank you – that looks like the appropriate program.I will try changing it.

    #26894
     claremcsharry
    Participant

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

    #26912
     sharmstr
    Moderator

    Honestly, 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 solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #26937
     sharmstr
    Moderator

    I 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 solution

    This 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.
    #26939
     sharmstr
    Moderator

    Here’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 solution

    This 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.
    #26956
     claremcsharry
    Participant

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

    #26972
     sharmstr
    Moderator

    I 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 solution

    This 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.
    #27043
     claremcsharry
    Participant

    Hi 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.

    #27046
     sharmstr
    Moderator

    Awesome. 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 solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #27049
     claremcsharry
    Participant

    Cool – will do!

    #27114
     propulsa
    Participant

    I 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.

    #27123
     sharmstr
    Moderator

    Fix 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 solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #27124
     sharmstr
    Moderator

    Here’s the fixed quotes

    COPY CODE
    
    
    
    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'
            )
        );
    }
    
    
    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #27221
     propulsa
    Participant

    I 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

    #27230
     sharmstr
    Moderator

    Can 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 solution

    This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com

    #27236
     propulsa
    Participant

    Ok. Got it. Finally I made it works. Thank you.

    #28748
     potion5
    Participant

    A 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;
    }
    
    #108136
     clubmasterminds
    Participant

    I 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

Viewing 20 posts - 1 through 20 (of 20 total)

The forum ‘KLEO’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?