Keyword

Retrieve K2 category ID in template

More
10 years 4 months ago #128506 by Dan T
Hi

I'm using the following script in my template index.php to assign a page class depending on which K2 category is being viewed.
Log in  or Create an account to join the conversation.

More
10 years 4 months ago #128507 by Lefteris
Replied by Lefteris on topic Re: Retrieve K2 category ID in template
Hi. Your code is incorrect. It does not take under consideration which page are you viewing currently. You just request the id variable from the URL. This could also be a category id when you are viewing a category page or a user id when you are viewing a user page. The code you provided will work only for item pages. So you will have probably to clarify on what pages you want this to be applied and the update the code accordingly.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
10 years 4 months ago #128508 by Dan T
Replied by Dan T on topic Re: Retrieve K2 category ID in template
ok, thanks for your reply. So I've tried a different approach and am calling the category using
Log in  or Create an account to join the conversation.

More
10 years 4 months ago #128509 by Lefteris
Replied by Lefteris on topic Re: Retrieve K2 category ID in template
No, this data is available only inside K2 views. This is how Joomla! is structured. The same goes for all other components. If you want this inside your template then you should modify your code a bit:
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');

if ($option == "com_k2" && $view == "item")
{
	$K2Itemid = JRequest::getInt('id');
	$db = JFactory::getDBO();
	$db->setQuery("SELECT catid FROM #__k2_items WHERE id = ".$K2Itemid);
	$K2Catid = $db->loadResult();
	$pageclass = "category-".$K2Catid;

}
else
{
	$pageclass = "not-k2";
}

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.