5

How can I add the category image to the categories listed using the J3.3.1 core module mod_articles_categories?

I think I have to create a module override in template/my-template//html/mod_articles_categories/ folder, but what are the code changes I have to make?

1
  • 1
    Yes, an override will be required....what have you tried so far?
    – Lodder
    Jul 4, 2014 at 9:59

3 Answers 3

4

In the template override file /html/mod_articles_categories/default_items.php I added the code

<img src="<?php echo $item->getParams()->get('image') ?>" width="40" height="40" border="0"/>

just after this line:

<a href="<?php echo JRoute::_(ContentHelperRoute::getCategoryRoute($item->id)); ?>">

That did the trick.

Offcourse the category image has to be set for each category shown, otherwise no image will display.

1
  • You can add a small PHP if condition to check if the category image is available, in order to avoid an empty <img> tag: <?php if ($item->getParams()->get('image')) { ?><img src="<?php echo $item->getParams()->get('image') ?>" width="40" height="40" border="0"/> <?php } ?>
    – johanpw
    Aug 7, 2014 at 3:07
1

If you add an image to your category description, and set the module parameter "Category Description" to yes, the description (if any) AND the image will be displayed in your module without any overrides.

If you need to add the category image somewhere else (e.g. above the title), you could try this in your override file template/my-template/html/mod_articles_categories/:

<?php
  $string = $item->description;
    $beg = substr($string,strpos($string,"<img"),strlen($string));
    //$catimg = substr($beg,0,$pos_srch = strpos($beg,">"));
    $catimg = substr($beg,0,$pos_srch = strpos($beg,">"));
  echo $catimg;
?>

This will echo the first image from the category description, including the <img> tag.

0

You can get the stored image path within your override using $item->getParams()->get('image') From there it should be easy to put that path into an img tag.

3
  • $item->getParams()->get('image') did not work for me, it returns nothing.
    – johanpw
    Aug 1, 2014 at 19:42
  • It only will return something if you actually have defined an image for the category in the categorys options tab. It will not detect an image in the description.
    – Bakual
    Aug 2, 2014 at 19:09
  • Yes, of course, you're right. I forgot that option.
    – johanpw
    Aug 7, 2014 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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