K2 Tags: The Benefits and How to Customise the K2 Tag Page

K2 Tags: The Benefits and How to Customise the K2 Tag Page

Tags in K2 are a useful tool when you have a large amount of categories and/or items. They are similar to K2 Categories, but with a slightly different purpose. This article looks at the benefits of using Tags in K2, but also how to use and customise the Tag pages.

The Benefits of K2 Tags in Joomla

Much like Articles in Joomla, a K2 Item can be assigned to only one category, which is problematic when you have an Item that covers more than one subject. This is where K2 Tags come in handy, because a K2 Item can be assigned to multiple Tags.

But Tags, in most cases, shouldn’t be the focus of your K2 content structure. The main focus will usually be to create a clean category structure that covers a broad grouping of your website’s content. Only after this should a Tag structure be considered, and if implemented, will usually only be to support your K2 category structure, not replace it.

The main benefit of Tags is that it allows you to create keywords based around your K2 Items. If, for example, you created a post about how to build a Joomla website, you may publish this in a K2 Category called ‘Joomla’, but then assign it to Tags such as ‘Joomla Templates’, ‘Joomla Extensions’, and ‘Joomla Web Hosting’.

So, using K2’s Tags feature in your website is something to consider, but there is a problem: compared to the impressive number of options available in the Category and Item view, K2 offers only basic configuration in terms of editing the layout and content of your Tag page, so you may need to edit the PHP file that controls how the Tags are displayed.

K2 Tags Configuration in Joomla

Before jumping in and hacking into the file that controls the Tags pages, it is definitely worth checking the available configuration options in the K2 parameters for Tags, as it’s possible that the options available are sufficient for what you want to achieve, meaning you don’t need to edit any files.

You can find the parameters by going to Components > K2 > Parameters.

The options it gives you are:

  • Item Count for Tag Listings - so you can set the number of Tag items that appear on the page
  • Item title - whether the Item titles should be displayed
  • Link on Item title - makes the Item titles link to the Item page
  • Item created date and time - determines whether to display the Item creation date
  • Item image - displays the Item image
  • Item intro text - displays the intro text of the Item
  • Item category - displays the category in which the Item is in
  • Item ‘read more…’ link - decides whether the ‘read more’ link is displayed
  • Item extra fields - displays any extra fields associated with any of the Items
  • Item ordering - lets you dictate the ordering of the Tag Items (ie. alphabetically or by newest first)
  • RSS feed (head) link / RSS feed icon - displays RSS feed links

As you can see, it does give you some of the most essential options, such as whether to display images or intro text, but if you were to set up your K2 Tag page using the above options, you will notice one big issue: it doesn’t display the Tag title.

Admittedly, if you wanted to add the Tag title to the K2 Tag page, you could create a Joomla menu item for each Tag, and then set the title to YES in the menu item’s parameters. But that’s quite a long-winded approach, especially if you’ve created a lot of Tags.

So now we come back to editing the PHP file that controls the Tag page.

How to Edit the K2 Tag Page in Joomla

To edit the K2 Tag page, the first thing you need to do is locate the tag.php file that controls it. Assuming you’re using a custom Joomla template with K2 pre-installed, this will be located in: templates/YOUR-TEMPLATE/html/com_k2/templates/default.

If you aren’t using a custom template, or are using a template that didn’t come with K2 pre-installed, the tag.php file will be located in: components/com_k2/templates. It is advisable, however, to create a template override in your Joomla template’s folder, rather than editing the core K2 files.

Open the tag.php file and you’re ready to start customising the layout and content of your K2 Tag page.

Adding the Tag Name to the K2 Tag Page

By default, the K2 Tag page doesn’t display the Tag name as a header, so this is probably the first thing you’ll want to change. You can do this by inserting the following code near the top of the page:

<h1><?php echo $this->escape($this->params->get('page_title')); ?></h1>

You will more than likely put this code underneath the k2Container div, but maybe elsewhere depending on where you want the Tag title to display. I have also wrapped H1 tags around it, because this will be the main heading for your page.

Also be aware that the Tag title will include 'Tag:' as a prefix before the Tag name. This is controlled in your K2 language file, within the brackets of K2_DISPLAYING_ITEMS_BY_TAG. You can either rename it to something more suitable, or, if you just want to display the Tag name on its own, edit the language file and leave it blank.

K2 Tag Page: Advanced Customisation

Adding the Tag name to the K2 Tag page is probably the most commonly sought after hack, but those with a decent knowledge of PHP may want more elaboration.

I’m sure the length of this article is boring you by now though (if you’re still here), so I’ll wrap this up without giving any more examples. But if you want more customisation - look through the tag.php file and study the code. Even if you’re a beginner with PHP, a lot of the code is common sense in terms of determining what it controls.

So, if you see anything within the file you no longer want - delete the code. If you want to include something within the Tag page that isn’t currently there (the social media buttons, for example) - find out which file the ‘something’ is stored in (most likely in category.php, category_item.php, or item.php), and copy it into the tag.php file. More than likely it will work.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.