- Posts: 2
COMMUNITY FORUM
Retrieve K2 category ID in template
- Dan T
-
Topic Author
- Offline
- New Member
Less
More
11 years 3 days ago #128506
by Dan T
Retrieve K2 category ID in template was created 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.
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.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
11 years 3 days 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.
Please Log in or Create an account to join the conversation.
- Dan T
-
Topic Author
- Offline
- New Member
Less
More
- Posts: 2
11 years 3 days 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.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
11 years 2 days 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";
}
Please Log in or Create an account to join the conversation.