How can you test if you're on the homepage in

2019-04-06 22:01发布

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

标签: php joomla
10条回答
Lonely孤独者°
2楼-- · 2019-04-06 22:30

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 ?>
查看更多
别忘想泡老子
3楼-- · 2019-04-06 22:37

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

$app = JFactory::getApplication();
if ($app->getMenu()->getActive()->home) {
    $homepage=true;
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-04-06 22:37

for Joomla 1.6 and 1.7 it would be as follows:

if(JRequest::getVar('view') == "featured" ) {
    //You are in!
}
else {
    //You are out!
}
查看更多
祖国的老花朵
5楼-- · 2019-04-06 22:43
if(JRequest::getVar('view') == "frontpage" ) {
    //You are in!
}
else {
    //You are out!
}
查看更多
Juvenile、少年°
6楼-- · 2019-04-06 22:46

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; ?>
查看更多
戒情不戒烟
7楼-- · 2019-04-06 22:48

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

查看更多
登录 后发表回答