Virginia Web Designers: Joomla, Drupal, Open-Source, CSS, and PHP Northern Virginia / Maryland / Washington DC web designers

11Mar/1010

Detect Joomla section ID when viewing an article

Sometimes when you’re coding a Joomla 1.5 template you may want to be know which section an article is in. For example, let’s say you have designed custom navigation buttons (not using any Joomla module, just your own graphics). If you have a button called “About Us”, you’d want it to stay activated when viewing any page in the About Us part of the site. To do this, create a section called About Us, create a category within that, and put all About pages in there. Now you can reliably detect your articles’ section IDs as a way of keeping your button activated on the correct pages.

Add this in your template PHP code;

$db =&JFactory::getDBO();
if(JRequest::getVar(‘view’)==’article’){
$query = “SELECT sectionid FROM #__content WHERE id=”.JRequest::getVar(‘id’);
$db->setQuery($query);
$sectionid = $db->loadResult();
}
if(JRequest::getVar(‘view’)==’category’){
$query = “SELECT sectionid FROM #__content WHERE catid=”.JRequest::getVar(‘id’);
$db->setQuery($query);
$sectionid = $db->loadResult() ;
}
//echo $sectionid;

Now you can use if statements like this;

if($sectionid == ’4′){
echo “this article is in section 4!”;
}

You can use this for customized navigation, for example, keeping certain buttons highlighted when you’re viewing articles within a certain section. It can be used to display a module only if you’re in a certain section, but it’s useful for more than just modules. Enjoy.

Filed under: Sundry Leave a comment
Comments (10) Trackbacks (0)
  1. this doesn’t work. tried it, throws an error

  2. my mistake, it works, but you have to change the quotation marks. Yours came into my editor in a wierd way so retyping them fixed it.

  3. sorry. WordPress doesn’t make it easy to copy and paste the code

  4. This sounds great to utilise to my new joomla site. I’m a newbie anyway.

    Stan, could you please let us know which quotation mark you used to make this script worked?

    Cheers!

  5. I have installed the 1.3.0 version of yvComment on Joomla 1.5 Beta2, and it is working okay in normal article views – I can write and view comments.
    My doubt is if this also should work in a “Category Blog Layout”. When I use the blog layout, I see this under each article in the blog: This is comment of “”
    If there is added any comments to the article in the normal view, I also see this in the blog layout: Comments (1)
    However I cannot read the comment or link to it from the blog. I would also like to have the option to add comments directly from the blog.
    Any ideas if this a only a local problem for me, or can someone give some hints if I need to modify something? (I am new to Joomla, so please explain in details)

  6. why are you using a beta version of 1.5?
    As for yvComment .. I have not used it. I use and like JomComment. Your question should be directed to yvComment support or their forum, I think.
    Maybe some of our readers know the answer?

  7. This doesn’t work. My $sectionid is empty after running the script.

  8. it is probably a problem with the quotation marks in this post. Try this version instead; http://www.plethoradesign.com/detect-joomla-section-id-when-viewing-an-article

  9. Nope nothing (btw 2 errors on that page: two dots: .. and no ; at end) Please try to add
    echo $sectionid; at the end, script shows nothing, so its empty. Is it important to put this code is head section, or something.

  10. OK try it now. Thanks for alerting me to the formatting error. I moved much of the blog over to the company site and in so doing noticed WordPress liked to add its own quotation marks in, which messed things up.
    I updated this post with the corrected code – you can also see http://www.plethoradesign.com/detect-joomla-section-id-when-viewing-an-article .. it is easier to copy the code from there.

    The code can go anywhere in the page … doesn’t have to be in the head. I use it to show certain menus on certain sections. Of course, with Joomla 1.6 this will be useless, since there are no sections in 1.6, only categories.


Leave a comment

(required)

Spam Protection by WP-SpamFree

No trackbacks yet.