Azharkamar.com

How to Check for Home Page in Joomla 1.6 using PHP

Tutorials
2 Jun 2011
2 comments

I was building this Joomla 1.6 site for a client and I needed to display a banner image only on the home page. So I was thinking – Use the Banners component, or do some coding instead? Components can be boring and I love coding (I think) so I chose the latter.

Then I’ll splatter the batter on some matter.

Ok so I decided to insert some magical codes into my Joomla template’s index.php file. The code would simply check if the user is currently viewing the home page, and then show the image banner. Else, no banner for you.

Checking for Home Page in Joomla 1.6

Here’s some PHP stuff to check if a page is the home page:

<?php
if(JRequest::getVar('view') == 'featured' ) :
    echo 'This is the front page';
else :
    echo 'This is NOT the front page';
endif;
?>

There’s another way to write the codes, but I don’t wanna use this one as it might not work in some cases. You can also use the default menu option to check the home page. If the menu is activated, the default item for menu would be home page.

Anyway, here it is:

<?php
$menu = JSite::getMenu();
if (JRequest::getInt('Itemid') == $menu->getDefault()) :
    echo 'This is the front page';
else :
    echo 'This is NOT the front page';
endif;
?>

By the way here’s how I implemented this check for my site if you wanna know:

<?php if(JRequest::getVar('view') == 'featured' ) : ?>
    <img src="images/banner_home.jpg" />
<?php endif; ?>

Short and sweet. Have fun!

Author - Azhar Kamar
2 Comments Revolving around this Article
  1. name

    your code fails sometimes, try instead:
    getActive() == $menu->getDefault()) {
    echo ‘This is the front page’;
    }
    ?>

    http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page



    Azhar Kamar replied:

    @name, thanks for your feedback!

Write a Comment! NOW! :)

Current ye@r *