Homepage Conditional Statements for Joomla 1.5 Templates

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only.

Jommla 1.5 the popular content management system (CMS) is built using PHP allowing you to use conditional statements within your templates to further control site. A common requirement is to determine if we are on the homepage. There are a few ways of doing this as outlined in this article using different conditions.

We can test to see if view is set to front page e.g. using the front page component. The front page component is used on the home page by default in Joomla.

<?php if (JRequest::getVar(‘view’)==‘frontpage’) { ?>
  <!–– Code that appears on the front page ––>
<?php } else { ?>
  <!–– Code that appears on all other pages ––>
<?php } ?>

If you are not using the font page component or are using it elsewhere on the site we can use the menu instead to check our location in the site.

<?php $menu = &JSite::getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()) { ?>
  <!–– Code that appears on the homepage ––>
<?php } else { ?>
  <!–– Code that appears on all other pages ––>
<?php } ?>

Finally we can check the URI

<?php $uri = $_SERVER[‘REQUEST_URI’]; ?>
<?php if ($uri == "/") { ?>
  <!–– Code that appears on the root domain ––>
<?php } else { ?>
  <!–– Code that appears on all other URI’s ––>
<?php } ?>

You would need to also add a rule to your .htaccess to rewrite http:⁄⁄www.domain.com/index.php to http:⁄⁄www.domain.com for this to be true. This statement will also need to be modified if you have Joomla installed in a subfolder.

This article was posted on 11 June 2010 in Code, Joomla

comments

What you have had to say about all this...

Great article. I really like it. Thanks for information a lot.

- Bekir Cem

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.