Getting the tags of an article from the template index.php

Everything to do with Joomla! 3.x templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
ichilver
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Fri Jan 28, 2011 3:13 pm
Location: Luton, Bedfordshire, UK
Contact:

Getting the tags of an article from the template index.php

Post by ichilver » Mon Jul 13, 2020 12:35 pm

Is there a way in the Template's index.php (in this case myNewTemplate) of checking if an article or any of its categories up to the root contain a TAG.

Let me explain further....

I have the following categories setup.

- Cat1 (with a Tag of Level1)
- Cat1.1 (A sub category of Cat1, with a Tag of Level2)

I have an article in the second category called Art1 and that also has a tag on it of SomeTag1

In my index.php of my template I want to check if the current article has either SomeTag1 direcly on it, and also the tags of the category and its parent categories.

Is this possible?

I did a dump of $this from the template and nothing stood out.

I basically wanted to add something like the below in my template

Code: Select all

<?php if isset($this->tags['SomeTag1') { ?>
  Do some HTML stuff here.
<?php } ?>
Any pointers?
Any snippet code?

Many thanks in advance.
Prolateral (http://www.prolateral.com) is a provider of integrated messaging.
Follow Prolateral on Twitter (http://www.twitter.com/prolateral)

 
ichilver
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Fri Jan 28, 2011 3:13 pm
Location: Luton, Bedfordshire, UK
Contact:

Re: Getting the tags of an article from the template index.php

Post by ichilver » Mon Jul 13, 2020 2:13 pm

### POSSIBLE SOLUTION ###

I have a partial solution to my question. I can now get the tags for the article but not the categories.

So I have created a function as below.

Code: Select all

function isTag($tagName) {
    // Get the ID for the current article
    $articleID = JRequest::getVar('id');

    // Get the Tags for the current article
    $itemtags = (new JHelperTags)->getItemTags('com_content.article', $articleID);

    foreach ($itemtags as $itemtag){
        if (strcasecmp($itemtag->title, $tagName) == 0) {
            return true;
        }
    }

    return false;
}
and from my template index.php I use it has follows

Code: Select all

<?php if (isTag("SomeTag1")) { ?>
    HTML stuff here
<?php } ?>
Is there a better way to do this?
A more J! way of doing it?

Any ideas on getting the Tags in the category of the article and its parent categories?

I was kinda hoping that at the article level the Tags would have been inherited and easy to retrieve in a template.
Prolateral (http://www.prolateral.com) is a provider of integrated messaging.
Follow Prolateral on Twitter (http://www.twitter.com/prolateral)

 

Post Reply

Return to “Templates for Joomla! 3.x”