Minor tweaks to the positions of modules

In recent years, often at Forum ask very similar questions about the publication of modules: how to show the module only for unregistered users, “” how to limit the visibility of the module for specific IP-address, etc. In this short article, I want to show the way to solve these problems by using templates Joomla.

The main idea of ??the proposed solutions, this announcement in the Joomla template modules in different positions depending on conditions. Ie if some condition is satisfied, then we display in the pattern one position, and if not, then another. Like this:

<? Php
  if (condition) {
    mosLoadModules ( 'User1' );
  } else {
    mosLoadModules ( 'User2' );
  }
?>

Let’s consider some simple examples.

The mapping module only on the main page

If you want to display a module on the site only on the main page, and skip the rest (including subsidiaries) pages, you can use the solution using templates Joomla. We put in the position of the template module to check the current components:

 
<? Php
  if (($ Option == '') || ($ Option == 'Com_frontpage')) {
    mosLoadModules ( 'User1' );
  }
?>

This test will show the position of the modules user1 only if the currently open main page.

Restriction of a module on IP

If you want to display a module on the site only for a specific IP-address or range of IP-addresses, you can use the solution using templates Joomla. We put in the position of the template module to check IP-address:

 
<? Php
  if ($ _SERVER['REMOTE_ADDR'] == '127.0.0.1') {
    mosLoadModules ( 'User1' );
  }
?>

This test will show the position of the modules user1 only if the IP-address of the client is 127.0.0.1. For other visitors to this position will be hidden, and therefore the modules published in it will be invisible.

The restriction of the module-type user

Suppose you want to display 2 different modules: one for registered users, and another – for unregistered. As is known, Publications ModuleAs access rights, you can select Public, Registered and Special. If we publish a module with access Public, it will show up and registered, and unregistered. How can that be? And again we come to help templates. Add the template checks the user type:

 
<? Php
  if (!$ My->id) {
    mosLoadModules ( 'User1' );
  } else {
    mosLoadModules ( 'User2' );
  }
?>

If the site comes not a registered or an unauthorized user, the template to load modules published in the field user1And, if registered – load the position user2.

Similarly, we can solve the problem of display module for a specific Joomla User Group. When you declare a template positions the module adds a little test:

 
<? Php
  if ($ My->usertype == 'Author') {
    mosLoadModules ( 'User1' );
  }
?>

In the example above, the modules published in a position user1Will be visible only to users of Author.

The mapping module only view the full text of the material

If you want to display a module on the site only on the preview page of the full text of the material, you must insert a check variable $ Task:

 
<? Php
  if (($ Option == 'Com_content') && ($ Task == 'View')) {
    mosLoadModules ( 'User1' );
  }
?>

This test will show the position of the modules user1 only if the currently open view of the full text of the material.

Comments are closed.