1

In Joomla 3.7.2, I have made an override of com_content/categories/default_items.php to show a default image if the category does not have an image like so:-

        <?php if ($item->getParams()->get('image')) : ?>
                <img src="<?php echo $item->getParams()->get('image'); ?>"/>
        <?php elseif (($item->getParams()->get('image')) == null) : ?>
                <?php echo "<img src=/defaultimage.png>"; ?>
        <?php endif; ?>

This works and displays the default image for all categories (parents and child) which do not have an image set but I would like to modify this to only show the default image on subcategories, therefore the default image would not be displayed for top-level (i.e. root) categories.

How can I achieve this within this scope? I have tried the following:-

        <?php if (($item->getParams()->get('image')) == null && ($item->parent_slug != '1:root')): ?> 
                <?php echo "<img src=/defaultimage.png>"; ?>
        <?php endif; ?>

Unfortunately this returns the following error:-

Undefined property: JCategoryNode::$parent_slug in /templates/t3_bs3_blank/local/html/com_content/categories/default_items.php on line 41

The source of default_items.php is here. Thanks in advance for your assistance.

2

$item->parent_slug doesn't exist. Do a print_r($item); to see what the $item object contains.

Either $item->parent_id; or $item->level; should grab you the information you need. The former will return "root" and the latter "1" if it is a root level category.

  • print_r($item); caused php max memory allocation to be hit, but print_r($item->parent_id); gave the hint. I can do if ($item->parent_id != 'root') – Grant G May 29 '17 at 22:15

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.