If Joomla Front page (homepage), do something

You can make Joomla website more dynamic, by showing specific content or adding custom CSS just on website’s front page. No, it doesn’t require 2 templates

Is this Front page?

Basic code, as shown at Joomla help, is

<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
} ?> 

For multi-language sites

Show different content based on selected language

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) {
echo 'This is the front page';
}
elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) {
echo 'Il sagit de la premi?re page';
}
?> 

Instead of fr-FR or en-GB you must use precise language code. Otherwise it won’t work.

Call to a member function getActive()

You forgot to define $menu, add

$menu = & JSite::getMenu();

If still doesn’t work, most probably you have mistakes in template

That still doesn’t work

If that doesn’t work, you can check view, by using built-in function getCmd(‘view’)

For Joomla 1.5.

 <?php if (JRequest::getCmd('view') == 'frontpage') { // do something} ?> 

Some templates may have defined function inside ja_templatetools.php, for example JA-purity has

 <?php function isFrontPage(){ return (JRequest::getCmd( 'view' ) == 'frontpage') ; } ?> 

so in this case can know, whether it’s Frontpage, by using code:

<?php if ($tmpTools->isFrontPage()) { // do something} ?> 

In 1.7 version, you can use the same code, except there is no view ‘frontpage’.
Based on what you show on homepage, getCmd(‘view’) will return different results. For example, Featured articles will return ‘featured’, Virtuemart FrontPage – ‘virtuemart’.
You can see, what view does Frontpage has, by pasting

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

after body tag in template’s index.php file. Refresh browser and somewhere in top of website you will see name of view.

If frontpage is article or last articles from some category, you better get

$menu->getActive()

to work. Because in this case getCmd(‘view’) will return same view also in articles/category (not frontpage).

This option really should be used as last resort, because it won’t allow much flexibility.

Examples

Add

<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
$frontpage="yes";
} ?>

to beginning of template’s index.php file

Every time, you want to check if user is on frontpage, use

<?php if ($frontpage) {//do something} ?>

Show text on

<?php if ($frontpage) {echo This is frontpage} ?>

Show module

<?php if ($frontpage) {echo '<jdoc:include type="modules" name="modulename" style="xhtml"/>';} ?>

Hide module

<?php if (!$frontpage) {echo '<jdoc:include type="modules" name="modulename" style="xhtml"/>'} ?>

only difference from previous example is little exclamation mark in if statement

Show long HTML code

<?php if ($frontpage): ?>
<div id="box1"><div>Content</div></div>
<div><div>Content </div></div>
<div id="box2">There can be anything</div>
<?php  endif; ?>

Add CSS class

<body<?php if (JRequest::getCmd('view') == frontpage)  {echo 'class="classname" '} ?>

Then of course define body.classname {} in your CSS file
Include Javascript
Paste this between head tags

<?php if ($frontpage") {echo '<script type="text/javascript" src="pathtoscripts/jquery.js"></script>';} ?>

As you see, after recognizing Frontpage, usage is unlimited. You can literally turn homepage’s design upside down, including or excluding specific content and applying CSS styles. Paraphrasing post’s title, If Joomla Front page (homepage), do anything 😉


Deprecated: Non-static method dc_jqslicksocial::footer() should not be called statically in /var/www/digitalette.com/wp-includes/class-wp-hook.php on line 298

Notice: Undefined index: show_post in /var/www/digitalette.com/wp-content/plugins/slick-social-share-buttons/inc/dcwp_social.php on line 805