English 中文(简体)
How can you test if you re on the homepage in Joomla?
原标题:
  • 时间:2009-11-18 20:28:48
  •  标签:
  • php
  • joomla

I m working on a Joomla site, and I need the front page to look slightly different from the rest of the pages, but not enough to warrant the use of two themes (it s a pain to have to update two stylesheets and two sets of images every time I want to make a small change).

My thoughts are to throw in a little test in the index.php of the template: if we re at the homepage, serve X, otherwise, serve Y. However, I m not entirely sure how to test this. I can t just use the URL because url.com/ and url.com/index.php and url.com/index.php? etc etc are all valid.

Does anyone know of a way to do what I m trying to do? Like a $_JOOMLA[ page ] variable or something convenient like that?

Thanks! --Mala

最佳回答
if(JRequest::getVar( view ) == "frontpage" ) {
    //You are in!
}
else {
    //You are out!
}
问题回答

To be shure that client is on homepage, you should test "is current page (Itemid) choosen as default menu item" like this code do (for Joomla 1.6, 1.7 and 2.5):

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

To find code for Joomla 1.5, look to http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page

This works for me, i had trouble using any other way

$app = JFactory::getApplication();
if ($app->getMenu()->getActive()->home) {
    $homepage=true;
}

For Joomla 2.5 and 3.x use the following code for a site with single language:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
    echo  This is homepage ;
}
?>

For multi-lingual sites, detecting homepage (front page) depends on the currently selected language, so you will need to use something like the following:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault(  en-GB  )) {
    echo  This is English homepage ;
}
elseif ($menu->getActive() == $menu->getDefault(  it-IT  )) {
    echo  This is Italian homepage ;
}
?>

For multi-lingual sites, you can also use the following code:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$lang = JFactory::getLanguage();
if ($menu->getActive() == $menu->getDefault($lang->getTag())) {
    echo  This is homepage ;
}
else {
    echo  This is not homepage ;
}
?>

Hope it helps!

for Joomla 1.6 and 1.7 it would be as follows:

if(JRequest::getVar( view ) == "featured" ) {
    //You are in!
}
else {
    //You are out!
}

For Joomla .6, nothing else than the this worked for me:

also you could define every page:

<?php 
$active = JFactory::getApplication()->getMenu()->getActive();
?>
 <body class="<?php echo $active->alias; ?> ">

use this one

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$lang = JFactory::getLanguage();
if ($menu->getActive() == $menu->getDefault($lang->getTag())) {
        echo  This is the front page ;
}
else {
        echo  Accueil ;
}
?>

In Joomla 3.x to show some content only on frontpage You can use

<?php $menu = JSite::getMenu();
    if ($menu->getActive() == $menu->getDefault()) : ?>
Some code here to show only on front page
<?php endif ?>

And to show something everywhere except frontpage just negate !=

<?php $menu = JSite::getMenu();
    if ($menu->getActive() != $menu->getDefault()) : ?>
    Some code here to show everywhere except frontpage
<?php endif ?>

As R.B. already pointed out, it s sensible to check the language of the menu item also, just in case there is more than one homepage as their is in multi-lingual sites.

<?php // Determine if we are on the homepage
$lang = JFactory::getLanguage();
$langTag = $lang ? JFactory::getLanguage()->getTag() : null;

$isHomepage = $langTag ? ($menu->getActive() == $menu->getDefault($langTag)) : ($menu->getActive() == $menu->getDefault()); ?>

Then where you want homepage-only content:

<?php if ($isHomepage) : ?>
     <div class="homepage-markup">

     </div>
<?php endif; ?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签