1

I know I can get the category id and parent category id, but what I really want is a category and any child categories.

I'm making a group of articles and I want to do an override to add a class to all articles in a category and all child categories, no matter how many child categories I add.

Is that possible? I don't want to do IF parent category id OR parent category id OR parent category id because if I do that I'll have to update it every time I add a new category.

I want to know an article categories and all subcategories

I checked out this article but it's not really the whole solution https://stackoverflow.com/questions/43647327/get-joomla-parent-category-id

Thanks in advance.

  • When you have an unknown number of child levels, it is often best to completely pull down all of the rows from the db table, then use php to recursively process the relationships. Only after having a good hard look at your realistic data and seeing your desired output, will anyone have a good chance of crafting an appropriate solution. You may like to update your question unless you've already managed to self-solve -- in which case, please post your solution and accept it. Please try to progress this question. – mickmackusa Mar 12 at 1:38
2

I faced a similar problem a while ago and not coming up with a good answer I came up with a straight forward messy answer. Behold:

public static function isArticleIdInThisCategoryId($article_id,$category_id)
{
    if(!$article_id){return false;}

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('a.catid AS parent1,b.parent_id AS parent2,c.parent_id AS parent3,d.parent_id AS parent4,');
    $query->from($db->qn('#__content','a'));
    $query->join('LEFT',$db->qn('#__categories', 'b') . ' ON (' . $db->qn('a.catid') . ' = ' . $db->qn('b.id') . ')');
    $query->join('LEFT',$db->qn('#__categories', 'c') . ' ON (' . $db->qn('b.parent_id') . ' = ' . $db->qn('c.id') . ')');
    $query->join('LEFT',$db->qn('#__categories', 'd') . ' ON (' . $db->qn('c.parent_id') . ' = ' . $db->qn('d.id') . ')');
    $query->where($db->qn('a.id') . " = " . $db->q($article_id));
    $db->setQuery($query);
    $result = $db->loadAssoc();

    if(empty($result)){
        return false;
    }

    for ($i=1; $i <= 4; $i++) { 
        if(!empty($result['parent' . $i]) && $result['parent' . $i] == $category_id){
            return true;
        }
    }
    return false;
}

It's a public static function because i had it in a helper class, but hopefully you can see the idea. Also note it only goes 4 parent categories deep, but should be easier enough to make deeper if needed.

  • Wow a beast of an answer! I'll test it. – Eoin Jun 29 '18 at 9:20
  • There must be a more efficient way – Eoin Jun 29 '18 at 9:20
2

Database method when you want to check against one category and all its children. Where $categoryId is your specified category.

// Get child category IDs
$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->select('sub.id')
    ->from('#__categories as sub')
    ->join('INNER', '#__categories as this ON sub.lft > this.lft AND sub.rgt < this.rgt')
    ->where('this.id = ' . (int) $categoryId);
$db->setQuery($query);
$categoryIds = $db->loadColumn();
// Add top category too.
$categoryIds[] = $categoryId;

Then, when displaying the article, add this check:

<?php if (in_array($this->item->catid, $categoryIds)) : ?>
    <?php // Article is in one of the categories, do stuff ?>
<?php endif; ?>
  • Cheers. Looks a lot shorter - no offence @jamesgarret I'll try both and see how they go. Thanks again. – Eoin Jun 29 '18 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.