Show a joomla article to registered users only, or a login screen if not logged in yes

坚强是说给别人听的谎言 提交于 2019-11-30 22:28:07

Im gonna answer my own question, because Im sure people will need this in the future, and my solution only involves a few rules of extra code and then you can set every article etc... to Registered and you'll see a login field when a user is not logged in.

In your templates index.php place this near the top, it gets your article's access level.

$article =& JTable::getInstance("content");
$article->load(JRequest::getVar('id'));
$cAccLevel = $article->get("access");

Then add a module position above your component, and only show it when your needed access level is > 1

<?php if($cAccLevel > 1): ?>
    <jdoc:include type="modules" name="LOGIN_MODULE_POSITION" />
<?php endif; ?>

Then add a login module in your module manager to LOGIN_MODULE_POSITION.

Voila... no routing needed etc... everything works out of the box, I chose to style away the logout box and action field like this:

.logout-button,
.actions{
    display:none;
}

Good luck!

  1. Create a new menu from menu manager, say it is named "hidden menu".
  2. Add any menu items that will be accessible only to registered users.
  3. Set the required access levels of these menu items ("Special" in this example, but it could also be "Registered"). Do NOT create a module for the "hidden menu". It will not be displayed on any page, so it doesn't need a module.
  4. Create your "real" menu (for example, "main menu") and the menu item that will display for all users (for example "Submit an Article"). The menu item will have a menu item type of "Alias". It's "Menu Item" parameter will be the "Submit an Article" menu item on the "hidden menu". The Access Level for this menu item will be "Public", since we want everyone to be able to see and use it.

  5. Create a module of type "mod_mainmenu" for this menu, just like you do for any menu.

  6. Create a login module and set the access level to "Public". Make sure the module is displayed only on the "Public" menu item and not the registred item, and select a visible position.

Now, when a guest (non-logged-in user) accesses the "Submit an Article" menu choice, it redirects them to blank page with the "Only for registred visitors" message. . If they log in successfully, they are taken to the desired page (in this case, "Submit an Article"). If there were already logged in, they go there directly.

If what you want is just to show a content if the visitor is a registered user, you can use this code:

$user =& JFactory::getUser();

    <?php
   if( !$user->guest ){ ?>

       [[what ever you want to do ]]
   <?php endif; ?> 

Hope that helps!!

Go menu CONTENT, create or edit a section to the items you want to protect.

You'll see an option: Access Level

There is 3 options:

Public, Registered and Special

Set it to: Registered

Then you'll need to change your articles for this section and that's all.

First of all you have to update your joomla installation to 2.5 (it's the latest version of joomla), 1.7 is no more supported by Joomla community.

There are several ways to set your viewing rights. K2 is a useful CCK component to do the job, also a useful technique that I use to follow is to "link" your article's categories in a menu and set specific permissions to the menu items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!