Menu Items Automatically Pointing to New Articles

From Joomla! Documentation

Revision as of 17:00, 31 October 2022 by Cmb (talk | contribs) (Several markup changes.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Sometimes a menu item should point to a new article as soon as the article is written, and this without having to edit the menu.

This can be done in Joomla 1.5 via the latestnews module and an HTML override:

1. Create a latestnews module to list all articles in the category which shall be added to the menu. The module output can be formatted by the stylesheet in the same manner as a 'real' menu item. (Use module style="xhtml" to get a simple <ul> list.)

2. Under Menu Assignment, configure this latestnews module to only display when the appropriate main menu item is active. This pseudo-menu will show up when the specific main menu item has been selected and it will disappear as soon as another main menu item is selected. Other main menu items may, of course, contain real menu items or point to additional latestnews modules.

Now we have a 'pseudo menu' which looks and behaves like a real menu. The only disadvantage is the lack of the "active" attribute which prevents us from visually marking the currently selected menu item.

To get this work as well, install an HTML override in the template:

3. In the active template's folder create the subfolder structure html/mod_latestnews (unless your template already contains this override).

4. In this folder html/mod_latestnews. . .

a) Create a file index.html. (copy one from any other folder).

b) Create a file default.php. This is the code which will override the standard latestnews output.

The content of default.php is:

<?php // @version $Id: default.php 9718 2007-12-20 22:35:36Z eddieajau $
  defined('_JEXEC') or die('Restricted access');
  if (count($list)){
  $uri = JFactory::getURI()->toString();
  echo '<ul class="latestnews'.$params->get('pageclass_sfx').'">';
  foreach ($list as $item){
    if(strpos($uri, $item->link) === false)
      $act = '';
    else
      $act = ' active';
    echo '<li class="latestnews'.$params->get('pageclass_sfx').$act.'">';
    echo ' <a href="'.$item->link.'">'.$item->text.'</a></li>';
    }
  echo '</ul>';
  }
 ?>

This checks if the link target is the same as the current page. If yes, it adds the active class attribute. This code could be enhanced (shorter, add additional output style options, prevent the unlikely case that the test gives a wrong positive result, etc.).