I have a membership site built off of WordPress and I want to create a navigation menu that is the same for all users, except for one tab that says “Your Subscriptions” and that tab will have a dropdown menu that shows what subscriptions the user has, which will be different for each user.
This is a small sample of what I have now :
if ( wp_emember_is_member_logged_in('19') ) {
wp_nav_menu( array( 'menu' => 'example1', 'menu_class' => 'sf-menu' ) );
That will check to see if a member has a certain membership level and then input a menu specific to that membership level. The only problem is I have like 20 elseif statements and 20 menus, which is not efficient at all.
Basically, I want to add single tabs to one menu instead of creating tons of different menus. I’m just not sure how to do it. Thanks!
Edited for question.
Hi there I am a little confused with what it is your after exactly, are you basically saying you want to have a menu for all users, and depending on a certain access level change 1 element in the menu??
If this is the case it looks like you need an ‘if’ statement to figure out (as you have done) if we are on the right access level and customise the menu accordingly. Basically it looks like you need two different arrays to build you menus:
You need to configure/set your menus themselves in this part of WP:
EDIT
It looks like you want to write nicer code and potentially not have 20 different if/else statements…. What I would do is this:
Have a folder with all your custom menus in it as separate PHP files which share the name with the group level for the user… ie
Then load the correct menu for the access level like:
Then whenever you create a new access level all you need to do is add a new file with the same name as the access level to the ‘custommenus’ folder, if the file hasn’t yet been created it will load a default menu!
Hope this helps 🙂 [PS this is untested]
ok, the best solution for you is to use custom menu’s i hope this suit your needs…
You can create two menus like this
in your functions.php:
then…where you you have the current menu – replace this:
With this:
Once you did that you can create the menu’s and have a different menu for logged in users and users that aren’t logged in.
For anyone else interested, I’ve been working with emagdnim to get an easy solution.
The best one found was using output buffering in the header.php file. In this way, we can render multiple menus using the wp_nav_menu(), but using output buffering we can break it up into pieces, and move those pieces around.
Here is the significant code (i.e. updated portion) for bottom-menu.php:
and the updated code for header.php (which loads bottom-menu.php)
Additional Information / Summary of Technique
This method takes one of two general menus and adds elements to it based on what should be loaded for each membership style. The “elements” are full menus created by wp_nav_manu(), but only one item. The names of the menus are also the names of the membership levels but with hyphens instead of spaces and all lowercase. By doing this, we do not need another table linking the seemingly arbitrary membership level ID with the name of the menu – you simply name the menus/memberships based on this system so it knows what to look for.
Furthermore, each added “menu” is actually a menu with only one, single menu item with the
<ul></ul>
tags removed so as to nest them into a sub-menu<ul>
below the desired parent element.After it is all said and done, we were able to use the wp menu system to dynamically display an infinite possibility of sub-menu items based on their membership level(s) and additional membership levels.
If all you need to add is one menu item at the end of the list, then you can hook up to a filter (wp_nav_menu_items) as described here. Then you can append a menu item with your subscription dropdown. Instead of using if statements, you can simply print the level.
Using this method, you can have a more fine tuned control over what your menu item will say and, if different levels of membership are similiar. ALSO, the switch statement is preferred to endless (if else)’s.
-Hope that helps
You could merge items from one menu into another using a
wp_nav_menu_objects
filter.You’d need to know the ID of the menu you wanted to merge in, and the ID of the menu item which you wanted the merged items to be a child of. Then you could just get the menu items, set their parent menu item and merge them with the parent menu. E.g.