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();

This entry was posted in Joomla and tagged , , , , , . Bookmark the permalink.

4 Responses to 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

  3. Honza says:

    For me worked this:

    category->id;?>

  4. Honza says:

    <q cite="category->id;?>”>

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>