Joomla 1.5 - Get Category and Section Functions - Template Tools


/ Published in: PHP
Save to your folder(s)

getCategory returns the category ID, getSection returns the section alias. Great for CSS overrides


Copy this code and paste it in your HTML
  1. function getCategory() {
  2. /** function for determining the category ID of a given page, referenced from:
  3. * http://forum.joomla.org/viewtopic.php?f=466&t=302292
  4. */
  5. $iId = JRequest::getVar('id',0);
  6. $database = &JFactory::getDBO();
  7. if(JRequest::getVar( 'view', 0) == "section"){
  8. return JRequest::getVar( 'id', 0);
  9. }else if(Jrequest::getVar( 'view', 0) == "category"){
  10. $sql = "SELECT id FROM #__categories WHERE id = '$iId'";
  11. $database->setQuery( $sql );
  12. $row=$database->loadResult();
  13. return $row;
  14. }else if(Jrequest::getVar('view', 0) == "article"){
  15. $temp = explode(":",JRequest::getVar('id',0));
  16. $sql = "SELECT catid FROM #__content WHERE id = ".$temp[0];
  17. $database->setQuery( $sql );
  18. $row=$database->loadResult();
  19. return $row;
  20. }
  21. }
  22.  
  23. function getSection() {
  24. /**
  25. function for determining the section alias of a given page
  26. */
  27. $menu = &JSite::getMenu();
  28. if ($menu->getActive() == $menu->getDefault()){
  29. return 'home';
  30. }
  31. $iId = JRequest::getVar('id',0);
  32. $database = &JFactory::getDBO();
  33. if(JRequest::getVar( 'view', 0) == "section"){
  34. $sql = "SELECT alias "
  35. . "FROM #__sections WHERE id = ".$iId
  36. ;
  37. $database->setQuery( $sql );
  38. $row=$database->loadResult();
  39. return $row;
  40. }else if(Jrequest::getVar( 'view', 0) == "category"){
  41. $sql = "SELECT a.alias "
  42. . "FROM #__sections as a, #__categories as b "
  43. . "WHERE (b.id = ".$iId ." "
  44. . "AND a.id = b.section)"
  45. ;
  46. $database->setQuery( $sql );
  47. $row=$database->loadResult();
  48. return $row;
  49. }else if(Jrequest::getVar('view', 0) == "article"){
  50. $temp = explode(":",JRequest::getVar('id',0));
  51. //$sql = "SELECT sectionid FROM #__sections as a, #__content as b WHERE b.sectionid = a.id AND b.id = ".$temp[0];
  52. $sql = "SELECT a.alias"
  53. . " FROM #__sections as a, #__content as b "
  54. . " WHERE (b.id = ".$temp[0] . " "
  55. . " AND a.id = b.sectionid)"
  56. ;
  57. $database->setQuery( $sql );
  58. $row=$database->loadResult();
  59. if (!$row)
  60. {
  61. return 'general'; // account for 'uncategorized' articles
  62. }
  63. return $row;
  64. }

URL: http://forum.joomla.org/viewtopic.php?f=466&t=302292

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.