user1154641
user1154641

Reputation: 5

Display category name in template

I am trying to display a category name in a module position.

I tried:

<?php echo $listing['Category']['title'];?>

It did not work.

I followed this link, but it shows the article title and I need the category one. I'm working on Joomla 1.7.

Upvotes: 0

Views: 14650

Answers (3)

Semmerket
Semmerket

Reputation: 83

Much more simple answer:

<?php echo $this->escape($this->item->category_title);?>

Upvotes: 6

Nick Yeoman
Nick Yeoman

Reputation: 51

Travega is really close, his code works on pages, but not on category pages.

When you use $id = JRequest::getString('id'); on a category page (such as a category blog or list page) the id of the category is returned. This means we need more context of the id variable, in this case the 'view'.

Here is my modified version of travega's code:


function getCategoryName() {
    //Modified from: http://stackoverflow.com/questions/8928967/joomla-display-catagory-name-in-template

    $db = &JFactory::getDBO(); 
    $id = JRequest::getString('id'); 
    $view = JRequest::getString('view'); 

    if ($view == 'category') {
        $sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
    } else {
        $sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
    }

    $db->setQuery($sql); 
    $category = $db->loadResult(); 
    return $category; 
}

Other relevant info:

I've only tested this on Joomla 2.5.3 on cat blog and cat list pages. I've not tested it on anything other than com_content component. This means it probably won't work on weblink, contact, etc pages as you may again loose the context.

Upvotes: 5

travega
travega

Reputation: 8415

As per the posters comment in the OP:

<?php 
    $db = &JFactory::getDBO(); 
    $id = JRequest::getString('id'); 
    $db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id); 
    $category = $db->loadResult();
    echo $category; 
?>

Upvotes: 5

Related Questions