Skip to main content
3 of 3
deleted 4111 characters in body

Edit category list to show items from a category and its subcategories

I'm trying to modify the Category List of the com_content component via override, so that it shows both the articles of the selected category and its subcategories.

I tried to edit the default_articles.php file (in category folder) adding this code:

if (!empty($this->items) && $this->params->get('show_subcats') != '0')
{
    $sub_categories = $this->children[$this->category->id];
    foreach($sub_categories as $sub_category){
        $subcategory_model = JModelList::getInstance('Articles', 'ContentModel', array('ignore_request'=>true));
        $appParams = JFactory::getApplication()->getParams();
        $subcategory_model->setState('params', $appParams);
        $subcategory_model->setState('filter.category_id', $sub_category->id);
        $subcategory_items = $subcategory_model->getItems();
        $this->items = array_merge($this->items, $subcategory_items);
    }
    //Sort new items by date
    usort($this->items, function ($a, $b) {
        $order_date = ($this->params->get('order_date') == 'published') ? 'publish_up' : $this->params->get('order_date');
        return strtotime($b->{$order_date}) - strtotime($a->{$order_date});
    });
}

But I noticed that it's a wrong idea, because pagination and filter are applied before my editing, and they don't consider my new $this->items array.

I was also thinking of editing the pagination and filtering in the default_articles.php, after my edit of $this->items, but I don't know how to do it and if it's possible. Do you have some advice or alternative solution to make a category and subcategories articles list?