Get the Category of Current Page

Everything to do with Joomla! 1.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Locked
jasonk1230
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Wed Aug 22, 2007 6:03 pm

Get the Category of Current Page

Post by jasonk1230 » Fri Oct 05, 2007 4:54 am

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!

maddrive
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Wed Sep 26, 2007 10:39 pm
Location: Moscow, Russia
Contact:

Re: Get the Category of Current Page

Post by maddrive » Sat Oct 06, 2007 5:27 am

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;			
	}					
}
?>
Last edited by maddrive on Sat Oct 06, 2007 5:31 am, edited 1 time in total.
Please read forum rules regarding signatures: http://forum.joomla.org/viewtopic.php?t=65

jasonk1230
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Wed Aug 22, 2007 6:03 pm

Re: Get the Category of Current Page

Post by jasonk1230 » Sat Oct 06, 2007 11:46 am

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!";
?>

User avatar
sarahwbs
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Mon May 08, 2006 7:27 pm
Location: Halifax, Nova Scotia, Canada
Contact:

Re: Get the Category of Current Page

Post by sarahwbs » Tue Dec 16, 2008 7:28 pm

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;
sarah adams
web developer & programmer

Signature rules: Literal URLs only - http://forum.joomla.org/viewtopic.php?f=8&t=65

bennip
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sat Jan 12, 2008 3:22 pm

Re: Get the Category of Current Page

Post by bennip » Tue Dec 16, 2008 9:19 pm

@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)

rjhoukje
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Dec 18, 2008 10:48 am

Re: Get the Category of Current Page

Post by rjhoukje » Thu Dec 18, 2008 10:49 am

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];

Fireflight
Joomla! Intern
Joomla! Intern
Posts: 65
Joined: Fri Mar 30, 2007 4:29 pm

Re: Get the Category of Current Page

Post by Fireflight » Mon Jan 19, 2009 3:34 pm

maddrive wrote:Had a similar problem a couple of days ago
Maddrive, how would you modify your code to display the section's name?

Fireflight
Joomla! Intern
Joomla! Intern
Posts: 65
Joined: Fri Mar 30, 2007 4:29 pm

Re: Get the Category of Current Page

Post by Fireflight » Wed Jan 28, 2009 6:38 pm

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

emodme
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Apr 30, 2008 5:11 pm

Re: Get the Category of Current Page

Post by emodme » Fri Feb 06, 2009 1:31 am

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.

markabey
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Dec 16, 2005 12:08 am

Re: Get the Category of Current Page

Post by markabey » Wed Mar 04, 2009 4:38 pm

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

User avatar
ksandven
Joomla! Explorer
Joomla! Explorer
Posts: 366
Joined: Fri May 02, 2008 10:35 pm
Contact:

Re: Get the Category of Current Page

Post by ksandven » Thu Apr 23, 2009 1:32 pm

This is great stuff, but how do I put the same info (category or section name) on category blog pages?

KenAdam
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Wed Apr 22, 2009 5:03 pm

Re: Get the Category of Current Page

Post by KenAdam » Fri Jun 05, 2009 9:01 am

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

grayz
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 128
Joined: Tue Dec 12, 2006 8:37 pm

Re: Get the Category of Current Page

Post by grayz » Fri Aug 20, 2010 4:47 am

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.
Developing: boragroconsult.com

ist_04
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Dec 12, 2008 7:54 pm

Re: Get the Category of Current Page

Post by ist_04 » Thu Nov 03, 2011 8:02 pm

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

dav3sh
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Mar 07, 2012 9:09 pm

Re: Get the Category of Current Page

Post by dav3sh » Wed Mar 07, 2012 9:30 pm

I have yhe same pronblem here.. I would like to get the category level 1 name.
Does anyone know the solution?


Locked

Return to “Templates for Joomla! 1.5”