2

我怎样才能只在joomla的主页上显示一条消息?我有兴趣在 site.com 上显示它,而不是在 site.com/index.php/page 或任何其他网站上显示它。

我已经测试了以下内容:

    <?php $app = JFactory::getApplication(); 
    $menu = $app->getMenu(); 
    $lang = JFactory::getLanguage(); 
if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>this is the homepage
    <?php endif; ?>

和这个

<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
    echo "this is the homepage";
}
?>

问题是我仍然可以在诸如http://site.com/index.php/category/id/78-article之类的页面上看到消息“这是主页”,这显然不是主页。似乎每当链接中有index.php时,上面的代码就认为它属于主页。

4

3 回答 3

2

这与链接中的“index.php”无关。相反,它与http://site.com/index.php/category/id/78-article上的链接没有与之关联的菜单项有关。要完全按照您的要求进行操作,您可能需要对代码进行一些处理,并检查以确保实际页面的信息与主页信息匹配:

$jinput = JFactory::getApplication()->input;
$menu = & JSite::getMenu();
$active = $menu->getActive();
$default = $menu->getDefault();
if (
    $active == $default && 
    $jinput->get('option') == $default->query['option'] && 
    $jinput->get('view') == $default->query['view'] && 
    $jinput->get('id') == $default->query['id']
) {
    echo "This is the homepage";
}

我正在检查默认菜单项选项(哪个组件)以及视图和 id 值与输入中设置的值。

http://site.com/index.php/category/id/78-article此链接会将 id 设置为 78,并可能会更改主页菜单中定义的视图和选项,因此触发器不会发生。

于 2013-03-07T22:12:22.347 回答
1

好的,只是一个想法,但请尝试:

if (preg_match('/index\.php$/',$_SERVER['PHP_SELF'])) {
  echo "this is the homepage";
 }
于 2013-03-07T22:00:13.280 回答
0

你应该试试这个。1. 从管理员部分检查已分配“主页”(这是主页)的菜单。复制那里显示的链接。会是这样的

index.php?option=com_somecomponent&view=someview
  1. 现在转到模板中的 index.php。
  2. 在那里写一个条件

    if(JRequest::getVar('option')=='com_something' && JRequest::getVar('view')=='someview'){    //show message    }
    

这应该为您解决问题!但请记住。如果您更改主页菜单,则必须相应更改。

于 2015-03-27T09:56:50.020 回答