Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla | PHP: Have a category blog listing / latest articles within the menu structure

So I have a menu where some of the menu items are dynamic category listings. Something like this within the left nav (for example):

Menu One
Category Two
  - Dynamic Article 1
  - Dynamic Article 2
  - Dynamic Article 3
Menu Three
Category Four
  - Dynamic Article 1
  - Dynamic Article 2
  - Dynamic Article 3  

EDIT

I think my description of the menu isn't quite clear, so I'll expand here a bit.

Let's say I have a left nav that looks really snazzy and is organized like this:

 - About Us
   - Jack
   - Joe
   - Our Dog
 - Success Stories
   - Sammy in South Dakota
   - Delilah in Delaware
   - Arty from Arkansas
   - ....
 - Contact Us
 - Recent Articles
   - 9/4
   - 9/2
   - 8/30
   - 8/27
   - 8/25
   - ...

And along the way it just made sense to turn Stories and Articles into their own category as they're updated frequently and the process of adding and removing menu items is too much maintenance. Let's also assume that in both the "Success Stories" and "Recent Articles" sections that I'd want the last 'x' articles, say 5.

If I just had one menu I could use Joomla's Recent News modules (which is similar to the "Category Listing" but will show in the menu area). But if you try to insert that it will list the articles either before or after your existing menu (for example, might be able to get it to do: About Us / Contact Us / Success Stories / Recent Articles).

I need it integrated within the menu structure, so that there is a mix of menu items that are static (one menu link that goes to one page) and dynamic (where the menu shows the last x articles from that category). That category listing needs to be called as a subroutine while the menu is being dynamically generated. Or a menu item (Success Stories) needs to be replaced by a category listing within the menu structure. Maybe a module position in place of a menu and then place the latest news there?


I found this article that said to build a plugin. This Joomla doc, though for 1.5, makes it seem that the latest news module will just plug into the menu - but that didn't work that way.

I've built super simple plugins and am just learning PHP and Joomla dev - how would I intercept the menu construction to plug in a category listing? Or if there's an "out of the box" way of doing it I'm all ears. If it is a plugin how would you intercept the menu?

My initial thought is to say if category = 15 then print out a module position of "category15." Something like this. No idea if I'm on the right track there.

As always, thank you!

like image 874
Gisto Avatar asked Aug 15 '12 23:08

Gisto


1 Answers

That's been missing in joomla since version 1. If you follow these steps you'll be set :)

We need a template override for mod_menu, simply copy file:

/modules/mod_menu/tmpl/default.php

to

/templates/[YOUR_CURRENT TEMPLATE]/html/mod_menu/default.php

NOTE: you may need to create folders html and mod_menu, if they don't currently exist

Find the following lines: (~ line 52)

in /templates/[YOUR_CURRENT TEMPLATE]/html/mod_menu/default.php

if (!empty($class)) {
    $class = ' class="'.trim($class) .'"';
}

and just after them insert the following code:

if(strpos($item->title, '#dynamic-cat') !== false)
{
    require_once(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'models'.DS.'articles.php');
    $model  = new ContentModelArticles();
    $config = JFactory::getConfig();
    if($spcCat = (int) str_replace('#dynamic-cat','', $item->title)) {
        $model->setState('filter.category_id', $spcCat);
    }
    $db     = JFactory::getDBO();
    $db->setQuery($model->getListQuery());
    foreach($db->loadObjectList() as $tItem) {
        $link   = $config->get('sef') ? str_replace($item->alias, $tItem->id.'-'.$tItem->alias, $item->route) :  'index.php?option=com_content&view=article&id='.$tItem->id.'&Itemid='.$item->parent_id;
        $iclass = $item->anchor_css ? 'class="'.$item->anchor_css.'" ' : ''; ?>
        <li <?php echo $class;?> ><a <?php echo $iclass; ?>href="<?php echo $link ?>"><?php echo $tItem->title; ?></a></li><?php
    }
    echo '</ul>';
    continue;
}

Menu item that will be replaced with dynamically created links

  1. Create a new menu item of type "Single Article"
  2. In "Required Settings" select any article from the category you'd like to use
  3. Set "Menu Title" to #dynamic-cat 71

     where 71 is Category ID (optional)
    
  4. Set "Parent Item" to corresponding menu item you'd like to insert the dynamic menu into

Just created menu item will be dynamically replaced with links to articles from choosen category (if any)

Let me know if there's any issue with this. I've been using this method on both SEF-enabled and SEF-disabled joomla > 1.6 sites

like image 157
WooDzu Avatar answered Sep 22 '22 16:09

WooDzu