Jump to content

Hide certain menu items for members or guests


bwakad
 Share

Recommended Posts

I recently started with the bootstrap 3 profile. In functions file there is this code (below) which is of course cool for displaying the menu. But I want to hide the login and register link to display in it, if they are as a member logged in.

In the Access tab of the login template, I have tried to un-check the "member" user roles in the template. But for what ever reason, I can't uncheck it unless I uncheck the "guest" role... which result in only superuser to access it.

If I uncheck both, and only check the "member", then guest is also checked automatically. Which result in everyone can access it.

Question, how to do this???


<?php
/*
Navigation for ProcessWire using the Bootstrap 2.2.2 markup
This menu was written by Soma based on work by NetCarver and a bit thrown in by Joss

Navigation Bootstrap 3 update by Damienov, with multi level dropdown support fix
Taken from 
*/

function renderChildrenOf($pa, $output = '', $level = 0)
{
    $output = '';
    $level++;

    foreach ($pa as $child) {
        $atoggle = '';
        $class = '';

        if ($child->numChildren && count($child->parents) == 1) {
            $class .= 'dropdown';
            $atoggle .= ' class="dropdown-toggle" data-toggle="dropdown"';
        } else if ($child->numChildren && count($child->parents) > 1 ) {
            $class .= 'dropdown-submenu';
            $atoggle .= ' class="dropdown-toggle"';
        } else if ($child->numChildren && $child->id != 1) {
            $class .= 'dropdown-menu';
        }

        // Makes the current page and it's top level parent add an active class
        $class .= ($child === wire("page") || $child === wire("page")->rootParent) ? " active" : '';
        $class = strlen($class) ? " class='" . trim($class) . "'" : '';

        if ($child->numChildren && count($child->parents) == 1) {
            // Add Caret if have children
            $output .= "<li$class><a href='$child->url'$atoggle>$child->title <b class='caret'></b></a>";
        } else if ($child->numChildren && count($child->parents) > 1) {
            $output .= "<li$class><a tabindex='-1' href='$child->url'$atoggle>$child->title</a>";
        } else {
            $output .= "<li$class><a href='$child->url'$atoggle>$child->title</a>";
        }

        // If this child is itself a parent and not the root page, then render it's children in their own menu too...
        if ($child->numChildren && $child->id != 1) {
            $output .= renderChildrenOf($child->children, $output, $level);
        }
        $output .= '</li>';
    }
    $outerclass = ($level == 1) ? "nav navbar-nav" : 'dropdown-menu';
    return "<ul class='$outerclass'>$output</ul>";
}
Link to comment
Share on other sites

Thanks Soma, but that is actually code which I can't use to separate menu items. Example:

About link - visible to guests and members

Login link - only visible for guests

Register link - only visible for guests

Profile - only visible for member

Edit profile - only visible for member

etc.

The function above is cool, but it just takes all pages (links) and displays them.

Link to comment
Share on other sites

Well, since there is not really an easy way of doing this from admin side, I made special pages not visible. I then use this code in my functions.inc :

if(!$user->isLoggedin()) {
    $menu = "string-with-markup-and-my-links-for-guests"; // login or register

} else {

    $menu = "string-with-markup-and-my-links-for-users"; // edit-profile or change-pass

}

In head.inc I use this in the section part of the top-bar:  echo $menu;

Special pages use my template: credentials.php. The page "Credentials" is not visible but still accesible. The template has a switch() to determine what get's included.

Because of the switch() I can set a default case if no other one get's included. And inside this default, I simply give some instructions and information about other stuff.

So much fun to play with PW!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...