Joomla: get category ID in template

I just needed to specify special layout for the articles of selected categories. I was looking for some solution online, but none of them worked. Mostly you can find this:

JRequest::getInt('catid');

or

JRequest::getInt('catid', '', 'get');

Maybe they worked in some other version of Joomla, but only working code I found is this (little modified by me to return integer values):


function getCategory() {
$iId = JRequest::getVar('id',0);
$database = &JFactory::getDBO();
if(JRequest::getVar('view', 0) == "section"){
return (int) JRequest::getVar( 'id', 0);
}else if(Jrequest::getVar( 'view', 0) == "category"){
$sql = "SELECT id FROM #__categories WHERE id = '$iId'";
$database->setQuery( $sql );
$row=$database->loadResult();
return (int) $row;
}else if(Jrequest::getVar('view', 0) == "article"){
$temp = explode(":",JRequest::getVar('id',0));
$sql = "SELECT catid FROM #__content WHERE id = ".$temp[0];
$database->setQuery( $sql );
$row=$database->loadResult();
return (int) $row;
}
}
$categoryId = getCategory();

Share this:

7 thoughts on “Joomla: get category ID in template

  1. wats up blogger. you have put up a nice blog and i genuinely liked reading your posts. i wonder how you will know all this stuff. i was pleased when i saw your blog. what blogging platform did you use for this blog. i’m using wordpress and i find it a bit tough. are you using wordpress too? if yes can you tell me about the themes and plugins you use for your blog. i will be grateful if you assist me on this.

  2. Hi Dominic,
    thx 4 ur comment. I am using this template:
    Twenty Ten 1.2 by the WordPress team
    it is basic template of the WordPress installation.
    And about plugins I would list those basic:
    All in One SEO Pack
    Custom Tag Cloud Widget
    FD Feedburner Plugin
    jQuery Lightbox
    ShareThis
    Syntax Highlighter for WordPress
    Twitter Widget Pro
    and finally
    WPtouch (this displays custom mobile theme if my blog is being accesed from mobile device)

    Cheers
    Luke

Leave a Reply

Your email address will not be published. Required fields are marked *

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