Conditional Logic for Menu

Posted in: Newspaper
Post count: 7

Hi

I want to use conditional logic on the MAIN MENU for LOGGED-IN & LOGGED-OUT members on my site. I have tried several pluggins including “IF MENU” & “MENU ITEM VISIBILITY CONTROL” but to no avail.

What is the best way to accomplish this?

Post count: 6600

Hi,

Follow these steps:

1. First you have to register the new-menu, for that go to td_menu.php file and add this line: http://screencast.com/t/WJy9nhodUS

'header-menu-login' => __( 'Header Menu login (main)', TD_THEME_NAME),

You will get this result: http://screencast.com/t/ihF4cmqRgq

2. Secound go to menu-header.php file and add this condition like, this: http://screencast.com/t/OtKQsOJosx0

if ( is_user_logged_in() ) {
                                wp_nav_menu(array(
                                    'theme_location' => 'header-menu-login',
                                    'menu_class'=> 'sf-menu',
                                    'fallback_cb' => 'td_wp_page_menu',
                                    'walker' => new td_tagdiv_walker_nav_menu()
                                ));
                            } else {
                                wp_nav_menu(array(
                                    'theme_location' => 'header-menu',
                                    'menu_class'=> 'sf-menu',
                                    'fallback_cb' => 'td_wp_page_menu',
                                    'walker' => new td_tagdiv_walker_nav_menu()
                                ));
                            } 

3. Do the same for mobile menu, into menu-mobile.php file: http://screencast.com/t/3CQa5fpkw

if ( is_user_logged_in() ) {
            wp_nav_menu(array(
                'theme_location' => 'header-menu-login',
                'menu_class'=> '',
                'fallback_cb' => 'td_wp_no_mobile_menu'
            ));

        } else {
            wp_nav_menu(array(
                'theme_location' => 'header-menu',
                'menu_class'=> '',
                'fallback_cb' => 'td_wp_no_mobile_menu'
            ));

        }

After this set which menu you want for logged visitors and which for unregistered visitors and test it.
Hope this helps!
Sorry for the delay!

Thanks

Post count: 27

THanks ! But I guess it doesn’t work in a child theme… Isn’t It ?

Post count: 6600

Hi,

Unfortunately it doesn’t because you can’t override those files via child theme. However you can do it using the main theme, make a copy of those files and replace them after an update.

Thanks

Post count: 17

hello, when i add the exact same code in my theme any link to admin panel goes 404. Any hint? I did as shown. I use the lastes update of the theme and wordpress. Thank u!

Post count: 7909

Hi,


@mike
I provided this topic just for reference and this code works for Newspaper 4(like I told you in the other topic). You can register the new menu like this – http://screencast.com/t/QfsK0NIYp3Lg and here you can add your menu using a condition(if you want) – http://screencast.com/t/x7P9EshsqKbF

Thanks!

Post count: 2

Hello,

I have tried it with Newspaper 6 but it doesn´t work.

Is there anyway to do it?

Thank you

Post count: 6535

Hi

The only change that must be done is at point 1: the new nav-menu must be registered in td-menu.php
like this: http://screencast.com/t/qqX2Ht3AFOlo The steps 2 and 3 are the same.

Thanks!

Post count: 2

Hello Andrei

It has worked quiet fine. But I need to make available the menu for 3 cases:

– Public menu (logged off users)
– Private users menu (logged in as Users role)
– Suscribers menu (logged in as Suscribers role)

Please, could you help me with it?

Thank you in advance

Post count: 6535

Hi

You need to register a new menu using the above method. Also you will need a condition which display that menu only if the current user is a subscriber. Your code will look like this: http://screencast.com/t/qwCCbvE1http://screencast.com/t/XnidFPu3nHhttp://pastebin.com/XC9BUxFJ

Thanks!

Viewing 10 posts - 1 through 10 (of 10 total)
The forum ‘Newspaper’ is closed to new topics and replies.