Page 1 of 1

Get the Category of Current Page

Posted: Fri Oct 05, 2007 4:54 am
by jasonk1230
I need a way to display the category name on every article in the section.  Is there an easy way to do this?

I've been researching, but all I could find were things related to catID (which seems to have disappeared in 1.5) and mainframe (which doens't seem to help).

Thanks!

Re: Get the Category of Current Page

Posted: Sat Oct 06, 2007 5:27 am
by maddrive
Had a similar problem a couple of days ago http://forum.joomla.org/index.php/topic,216820.0.html

Here is the solution I found (maybe there's a better way):

Code: Select all

<?php
$db = &JFactory::getDBO();				
			
$option	= JRequest::getCmd('option');
$view	= JRequest::getCmd('view');

$temp	= JRequest::getString('id');
$temp	= explode(':', $temp);
$id	= $temp[0];
							
/* Checking if we are making up an article page */
if ($option == 'com_content' && $view == 'article' && $id)
{				
	/* Trying to get CATEGORY title from DB */
	$db->setQuery('SELECT cat.title FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);	
	$category_title = $db->loadResult();
					
	/* Printing category title*/
	if ($category_title) 
	{
	    echo $category_title;			
	}					
}
?>

Re: Get the Category of Current Page

Posted: Sat Oct 06, 2007 11:46 am
by jasonk1230
I actually ended up finding a hack to use in my template, using the $mainframe variable for each page.

Here's what I used:

Code: Select all

<?php $pathway =& $mainframe->getPathway();
$items   = $pathway->getPathWay();
if ($items[0]->name) {
	echo stripslashes(htmlspecialchars($items[0]->name));
} else {  //ie on the homepage
	echo "Welcome!";
?>

Re: Get the Category of Current Page

Posted: Tue Dec 16, 2008 7:28 pm
by sarahwbs
Perhaps my solution is far too simplistic, but this is how I'm doing this:

Code: Select all

$contentid = JRequest::getVar('id', '');
$my_query = "SELECT cc.id category FROM jos_content c, jos_categories cc WHERE c.catid = cc.id and c.id = $contentid";
$my_result = mysql_query ($my_query);
if ($my_result)
{
  $my_row = mysql_fetch_assoc($my_result);
  $category = ($my_row[category]);
}
else $category = 0;

Re: Get the Category of Current Page

Posted: Tue Dec 16, 2008 9:19 pm
by bennip
@sarahwbs

well, it works for your site, so it's good for your site, even if it's simplistic :)

It's the same code as maddrives', except that:

- $db->setQuery blabla is the Joomla way to it. For example, my tables have another prefix than jos_ in the configuration (#__ will be replaced by that prefix). Additionally, some may use (some day ?) other Databases then MySQL, as $db aims to be a generic interface for all.

- maddrive takes some other precautions, e.g. making sure that the id is an article (and not a contact, for example)

Re: Get the Category of Current Page

Posted: Thu Dec 18, 2008 10:49 am
by rjhoukje
This works great for me:
$itemID = JRequest::getVar('Itemid');

global $database;
$id = intval( mosGetParam( $_REQUEST, 'id', 0 ) );
$sql = 'SELECT c.title, s.title, cat.title '
. 'FROM #__categories AS cat, #__content AS c, #__sections AS s '
. 'WHERE cat.id = c.catid AND c.id = ' . $id . ' AND c.sectionid = s.id';
$database->setQuery( $sql );
$row = $database->loadRow();
$title = $row[0];
$section = $row[1];
$category = $row[2];

Re: Get the Category of Current Page

Posted: Mon Jan 19, 2009 3:34 pm
by Fireflight
maddrive wrote:Had a similar problem a couple of days ago
Maddrive, how would you modify your code to display the section's name?

Re: Get the Category of Current Page

Posted: Wed Jan 28, 2009 6:38 pm
by Fireflight
I found the solution to my question here. This is how you display the section name in a category blog layout.

http://forum.joomla.org/viewtopic.php?p ... 5#p1009495

Re: Get the Category of Current Page

Posted: Fri Feb 06, 2009 1:31 am
by emodme
This is a modification to "maddrive" version, it grabs both the Category alias and Section alias, you can change it yourself if you wish to use the title, just add cat.title, sec.title to the SELECT section of the SQL query. Then add additional variable names and place the items into them from the $category array.

Code: Select all

$db = &JFactory::getDBO();            
$temp   = JRequest::getString('id');
$id   	= $temp;
$db->setQuery('SELECT cat.alias, sec.alias FROM #__categories cat LEFT JOIN #__content cont ON cat.id = cont.catid LEFT JOIN #__sections sec ON sec.id = cont.sectionid WHERE cont.id='.$id);   
$category = $db->loadRow();	   
if (is_array($category)) {
    $cat = $category[0];  
    $sec = $category[1];         
}           
This has been tested to work in Joomla 1.5.X only.

Re: Get the Category of Current Page

Posted: Wed Mar 04, 2009 4:38 pm
by markabey
Thanks emodeme, that was really useful

I wanted a quick way to change stylesheet on pages in certain categories, put directly in my template index.php header (not the most efficient but quick)

However when there was no category ID (when someone views the front page) the SQL threw out an error so I added this line into line 3

Code: Select all

if ($temp == '') { $temp = '56';}
item id 56 was something on my front page

Thanks

Re: Get the Category of Current Page

Posted: Thu Apr 23, 2009 1:32 pm
by ksandven
This is great stuff, but how do I put the same info (category or section name) on category blog pages?

Re: Get the Category of Current Page

Posted: Fri Jun 05, 2009 9:01 am
by KenAdam
I wanted to control a "mini" google map in a module depending on which category (actually location) was selected, and I neede this work on both the category blog and article pages.
Based on the code above and a bit of investigation, I ended up with:

Code: Select all

<?php
  $db = &JFactory::getDBO();            
  $option = JRequest::getCmd('option');
  $view = JRequest::getCmd('view');
  $temp = JRequest::getString('id');
  $temp = explode(':', $temp);
  $id = $temp[0];
  if ($option == 'com_content' && $id)
  {
    /* Checking if we are making up an article page */
    if ($view == 'article')
    {            
      /* Trying to get CATEGORY title from DB */
      $db->setQuery('SELECT cat.title FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
      $category_title = $db->loadResult();
    }
    /* Checking if we are making up a category page */
    if ($view == 'category')
    {            
      /* Trying to get CATEGORY title from DB */
      $db->setQuery('SELECT cat.title FROM #__categories cat WHERE cat.id='.$id);   
      $category_title = $db->loadResult();
    }
    /* Printing category title*/
    if ($category_title) 
    {
      echo $category_title;         
    }
  }               
?>
Note that the "explode" (from maddrive's post, but omitted later) is required for those pages which have number:alias in the id parameter.
I'm fairly new to this, so I;d be interested if anyone can offer improvements.
Regards,
Ken

Re: Get the Category of Current Page

Posted: Fri Aug 20, 2010 4:47 am
by grayz
maddrive wrote:Had a similar problem a couple of days ago http://forum.joomla.org/index.php/topic,216820.0.html

Here is the solution I found (maybe there's a better way):
I also needed to solve this issue, my task was to retrieve page meta keys and/or page title. Here is the code I use (without querying the database)

Code: Select all

    function getTitleMeta() {
        
        $option	= JRequest::getCmd('option');
        $doc =& JFactory::getDocument();
        
        if ($option == 'com_content' && $doc) {     
            $result['metakeys'] = '';
            $result['metakeys'] = $doc->getMetaData('keywords');
            $result['pagetitle'] = '';
            $result['pagetitle'] = $doc->getTitle();
            return $result;
        } else {
            return FALSE;
        }
    }
Maybe it would be useful for somebody.

Re: Get the Category of Current Page

Posted: Thu Nov 03, 2011 8:02 pm
by ist_04
This works in 1.7 also.

To take it a step further, how could we incorporate the Parent level +1 of the top level Cat?

For example, if we have:

Cat A
Sub Cat 1
Sub Cat 2
Sub Cat 3

Any article/category in Sub Cat 1 would show the Sub Cat 1 title, not Cat A

Re: Get the Category of Current Page

Posted: Wed Mar 07, 2012 9:30 pm
by dav3sh
I have yhe same pronblem here.. I would like to get the category level 1 name.
Does anyone know the solution?