Skip to content

Instantly share code, notes, and snippets.

@Ulv
Created May 25, 2014 08:39
Show Gist options
  • Select an option

  • Save Ulv/295c35467d19ce13555e to your computer and use it in GitHub Desktop.

Select an option

Save Ulv/295c35467d19ce13555e to your computer and use it in GitHub Desktop.
joomla: fetch last 4 articles from current category subcategories
/*
* get subcategory ids
*/
$catID = $this->category->id;
$categories = JCategories::getInstance('Content');
$cat = $categories->get($catID);
$children = $cat->getChildren();
$children = array_map(function($el) {
return (array)$el;
}, $cat->getChildren());
$ids = array_column($children, 'id');
if (is_array($ids)) {
$ids = array_merge(array($this->category->id), $ids);
} else {
$ids = array($this->category_id);
}
/*
* get articles
*/
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__content'))
->where($db->quoteName('catid') . ' in (' . implode(',',$ids) . ')')
->order('created DESC');
$db->setQuery($query, 0, 4);
$articles = $db->loadObjectList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment