Alter webpage title on Joomla and K2 for SEO purposes

Recently, I  had to alter the <title> tags of a website built with Joomla CMS (version 1.5.x) and the powerful K2 content component, for SEO purposes. The problem was that the sitename that you define on the Joomla site settings was prepended to all title tags in all pages. For example if we suppose that the site name is “Sample site name”, the titles on my webpages where as following:

  • Homepage: Sample site name
  • Article pages: Sample site name – Article Tile

This is unwanted behaviour for SEO (and not only SEO) reasons. I don’t know if this is an issue only for websites using the K2 content component or a genaral Joomla issue. Anyway, the desired behavior is something like the following:

  • Homepage: Sample site name
  • Article pages:  Article Tile – Sample site name

There is little information on how to achieve that on the web when you use K2. So I thought I should share the way I did it. The following code is included in the index.php file of your template, somewhere in or before the tag:

$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$app = JFactory::getApplication();
if ($option=="com_k2" && $view=="item") {
    $ids = explode(':',JRequest::getInt('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance('k2item', 'Table');
    $mySEOtitle = $article->get("title");
    $mySEOimage = JURI::base()."media/k2/items/cache/".md5("Image".$article_id)."_M.jpg";
    $this->setTitle( $mySEOtitle.' - '.$app->getCfg('sitename') );
} else {
	$mySEOtitle = $this->getTitle();

Some things to note:

  • the following line of code checks so that the following changes apply only to the pages that display a K2 item. This is to differentiate from the homepage
if ($option=="com_k2" && $view=="item") {
  • the following 2 lines is how to get a K2 item from the database using the Joomla framework
   $article =& JTable::getInstance('k2item', 'Table');
  • bonus: the way to get the K2 item image. It is not stored in the db but in the filesystem using a filename that is the MD5 hash of  the Image Id and the word “Image” (eg “Image21”) and appending the size of the image to get (eg. _M.jpg, _L.jpg, _XL.jpg)
	$mySEOimage = JURI::base()."media/k2/items/cache/".md5("Image".$article_id)."_M.jpg";

I hope this helps someone as if it could have helped me. I would love to help with any questions you have!


  1. Thanks for the SEO explained for k2.

    I would like to ask, How can I add the meta description and meta keywords to the K2 content module, when I show this module on the front page.

    The K2 item is showing both meta(s) on item page, but when I need it on front page via K2 content module, it does not get the meta data from the k2 items. I did not find solution to this on the internet.


  2. Hi, the metatags on the homepage are not added through the K2 module. They are changed from the Joomla: Site-> Global Configuration menu

  3. Hello. Please tell me. How to get a category of material?

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.