• Resolved dave1234

    (@dave1234)


    I’m using these new custom menu things for my top nav menu, and I want to hide one of the menu items from users who are less than an Author.

    I’m told that can’t be done with any existing feature of WordPress or the custom menus system, so I guess I have to do a hack?

    Can anyone help me out with this? So far all I have found is that maybe I need to do the hack in nav-menu.php or nav-menu-template.php.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dave1234

    (@dave1234)

    Hum, well in my theme’s header.php there’s this:

    <?php /* The navigation menu */ ?>
            <?php
            /* Header menu */
            $args = array(
                'container' => '',
                'menu_id' => 'header-menu',
                'menu_class' => 'menu clearfix',
                'fallback_cb' => 'graphene_default_menu',
                'depth' => 5,
                'theme_location' => 'Header Menu',
            );
            wp_nav_menu(apply_filters('graphene_header_menu_args', $args));

    So it seems to me like somehow right there I could add code to say Don’t show a certain menu item (by id or slug name or something) when the user is less than an Author.

    Could use help on doing that though…

    Marion Dorsett

    (@figmentthinking)

    I would suggest making a simple child theme using your current theme as the parent. That way your parent theme remains intact in case of an update (like the twentyten theme every time you update WordPress.)

    I’ve setup an example child theme that will do what you want, and you can download it from here:

    https://www.figmentthinking.com/ft_downloads/author-theme.zip

    This example uses the twentyten theme for the parent, so you’ll need to make the necessary changes to the example files to get them to work with your theme.

    The key is to register two menus, then you can set them in the header.php file of the theme to show the different menus based on the conditions you set.

    Thread Starter dave1234

    (@dave1234)

    Many Thanks!

    Unfortunately I couldn’t just use that in a child theme as is, because I didn’t really know about child themes and I’ve already hacked up my theme pretty good. So I adapted what you wrote into a hack to my theme.

    I just had to change my code to this in header.php:

    if(current_user_can(edit_posts))
    				{
    					/* Header menu */
    					$args = array(
    							'container' => '',
    							'menu_id' => 'header-menu',
    							'menu_class' => 'menu clearfix',
    							'fallback_cb' => 'graphene_default_menu',
    							'depth' => 5,
    							'theme_location' => 'contrib-menu',
    					);
    					wp_nav_menu(apply_filters('graphene_header_menu_args', $args));
    				}
    				else //normal visitors will see this
    				{
    					/* Header menu */
    					$args = array(
    							'container' => '',
    							'menu_id' => 'header-menu',
    							'menu_class' => 'menu clearfix',
    							'fallback_cb' => 'graphene_default_menu',
    							'depth' => 5,
    							'theme_location' => 'Header Menu',
    					);
    					wp_nav_menu(apply_filters('graphene_header_menu_args', $args));
    				}

    And this in functions.php:

    // This theme uses wp_nav_menu() in one location.
    	register_nav_menus( array(
    		'Header Menu' => __('Header Menu', 'graphene'),
    		'secondary-menu' => __('Secondary Menu', 'graphene'),
    		'contrib-menu' => __('Contrib Header Menu', 'graphene'),
    	) );

    So, basically just adding the contrib menu in the code, and then of course I had to make a new menu in the dashboard for the Contrib Header Menu.

    The fact that menu_id was set to ‘header-menu’ was super confusing to me, since it has this other thing called ‘secondary-menu’. But apparently those 2 things are not the same type of thing at all, ‘header-menu’ is do to with the style id or something, and you can see his naming convention is just not uniform (the header menu is called “Header Menu”, yet the secondary menu is called “secondary-menu”.

    I was still pretty confused until just writing this reply now, now I see that my new “contrib-menu” is the counterpart to the original “Header Menu”, and the second instance of ‘Header Menu’ in the code “‘Header Menu’ => __(‘Header Menu’, ‘graphene’),” is the text that shows up in the Menus dashboard. If you see what I mean, the confusing part is the usage in header.php of header-menu vs contrib-menu.

    I know that hacking the base theme instead of using a child theme is a bad way to go about things, but I’ve hacked up my theme enough now that it would take some doing to revert it and switch the changes to a child theme (compounded by the fact that I have no idea how to make a child theme although I’m sure I could learn).

    Thanks again

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hide a menu item from regular users’ is closed to new replies.