DART Creations

#71 - Hide a menu item when user logs in

PDF Print E-mail

#71 - Hide a menu item when user logs in

There are times when you need to hide specific menu items when a user has logged on to your Joomla website. This cannot be done using standard functionality, we the following is a simple workaround to allow you to cheat by hiding the menu item when a user logs in. 

If you want to hide a whole module eg the menu you can used the trick here: #3 - Hiding a menu once logged in

So what do we do to hide the menu item for logged in users? A combination of using the Joomla framework (getUser) and a CSS trick (display:none) to hide items. In your template place the following code:

<?php
$user =& JFactory::getUser();
if( !$user->guest ) ?>
<style type="text/css">
.item10 { display:none; }
</style>

<?php endif; ?>

replace the 10 in .item10 with the id of the menu item. 

NOTE: this only works on the menu if the menu's module is set to 'List'. If you are familiar with CSS you can use it / extend for the style of any menu item you choose
 
Comments (6)
Only registered users can write comments!
  • wexplorer18  - where do I have to add the code?

    Hello,
    thank you very much for your tip
    That's exacly what I was looking for
    for the Log-in menu item of the
    restricted area.
    where do I have to add the peace of code above? I tryed it at different positions
    in the file index.php with no success :(

    Thank you, regards

  • joestange

    Your code breaks the site. It gives a parse error. I have tried it on 4 sites.

  • DART

    Well, i've tested it and it works. Unfotunately, templates are typically so different from each other, that what works on one, does not necessarily work on another. The code here basically gives you a hint on the direction you need to take when trying to do this.

  • magicflyer  - re:
    joestange wrote:
    Your code breaks the site. It gives a parse error. I have tried it on 4 sites.

    I got the parse error as well... changed the line:

    if( !$user->guest ) ?>

    to

    if( !$user->guest ) : ?>

    and this worked like a champ. Thanks DART!

  • rad02  - re:
    DART wrote:
    NOTE: this only works on the menu if the menu's module is set to 'List'. If you are familiar with CSS you can use it / extend for the style of any menu item you choose


    Can you please give an indication how the code can modified for usage with the following Menu Styles :) ?

    Legacy - Vertical
    Legacy - Horizontal
    Legacy - Flat List

    Thanks :)

  • rleathers  - MetaMod

    I know this is late but when you want to hide anything based on user role, logged in status, geo location, etc. try MetaMod.
    nolinkspls://zzz.metamodpro.com/

Resources

Translate

English French German Italian Portuguese Russian Spanish