Advertisement
Get the Category of Current Page
Moderator: General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
-
- Joomla! Apprentice
- Posts: 10
- Joined: Wed Aug 22, 2007 6:03 pm
Get the Category of Current Page
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!
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!
Advertisement
-
- Joomla! Apprentice
- Posts: 20
- Joined: Wed Sep 26, 2007 10:39 pm
- Location: Moscow, Russia
- Contact:
Re: Get the Category of Current Page
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):
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
-
- Joomla! Apprentice
- Posts: 10
- Joined: Wed Aug 22, 2007 6:03 pm
Re: Get the Category of Current Page
I actually ended up finding a hack to use in my template, using the $mainframe variable for each page.
Here's what I used:
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!";
?>
- sarahwbs
- 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
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
web developer & programmer
Signature rules: Literal URLs only - http://forum.joomla.org/viewtopic.php?f=8&t=65
-
- Joomla! Apprentice
- Posts: 8
- Joined: Sat Jan 12, 2008 3:22 pm
Re: Get the Category of Current Page
@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)
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)
-
- Joomla! Fledgling
- Posts: 1
- Joined: Thu Dec 18, 2008 10:48 am
Re: Get the Category of Current Page
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];
$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];
-
- Joomla! Intern
- Posts: 65
- Joined: Fri Mar 30, 2007 4:29 pm
Re: Get the Category of Current Page
Maddrive, how would you modify your code to display the section's name?maddrive wrote:Had a similar problem a couple of days ago
-
- Joomla! Intern
- Posts: 65
- Joined: Fri Mar 30, 2007 4:29 pm
Re: Get the Category of Current Page
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
http://forum.joomla.org/viewtopic.php?p ... 5#p1009495
-
- Joomla! Fledgling
- Posts: 3
- Joined: Wed Apr 30, 2008 5:11 pm
Re: Get the Category of Current Page
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.
This has been tested to work in Joomla 1.5.X only.
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];
}
-
- Joomla! Apprentice
- Posts: 5
- Joined: Fri Dec 16, 2005 12:08 am
Re: Get the Category of Current Page
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
item id 56 was something on my front page
Thanks
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';}
Thanks
- ksandven
- Joomla! Explorer
- Posts: 366
- Joined: Fri May 02, 2008 10:35 pm
- Contact:
Re: Get the Category of Current Page
This is great stuff, but how do I put the same info (category or section name) on category blog pages?
-
- Joomla! Apprentice
- Posts: 10
- Joined: Wed Apr 22, 2009 5:03 pm
Re: Get the Category of Current Page
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:
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
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;
}
}
?>
I'm fairly new to this, so I;d be interested if anyone can offer improvements.
Regards,
Ken
-
- Joomla! Enthusiast
- Posts: 128
- Joined: Tue Dec 12, 2006 8:37 pm
Re: Get the Category of Current Page
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)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):
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;
}
}
Developing: boragroconsult.com
-
- Joomla! Apprentice
- Posts: 15
- Joined: Fri Dec 12, 2008 7:54 pm
Re: Get the Category of Current Page
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
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
-
- Joomla! Apprentice
- Posts: 5
- Joined: Wed Mar 07, 2012 9:09 pm
Re: Get the Category of Current Page
I have yhe same pronblem here.. I would like to get the category level 1 name.
Does anyone know the solution?
Does anyone know the solution?
Advertisement