Tech Tee Shirts Explained »

How to detect page type in Joomla

Sometimes in Joomla code, you want to determine what page type is being displayed.  This is also known as the "view".  You can use getVar method of the JRequest object as in ...

<?php
echo JRequest::getVar('view');
?>

To test this out, put this code in near the bottom of the index.php of your template (just above the closing body tag) and you should see it return "frontpage", "section", "category", or "article" depending on the page type.

Note: The index.php template file that I am referring to is typically found in <your joomla install dir>/templates/<your template dir>/index.php.

Special Display for Home Page Example

As an example, suppose you want to display something different on the home page than in your other internal pages, then you can write this PHP code in your index.php template file.

<?php
if ( JRequest::getVar('view') == 'frontpage' ) {
?>

<!-- display your home page HTML here -->
<?php
} else {
?>

<!-- display your internal page HTML here -->
<?php } ?>

Detect what section article is categorized in 

The getVar('view') method can also tell you what section the article is categorized in.  See article "Find Section Name" for details.

Determine Page using the Menu Alias

If you want to select pages based on their menu alias, you can try the following code...

$theMenu = JSite::getMenu();
$theActiveMenu = $theMenu->getActive();
if ( strpos($theActiveMenu->alias, 'wii') !== false ) {
/* you are on a page where menu alias has "wii" in it */
}