【问题标题】:How to display a message on joomla homepage only?如何仅在 joomla 主页上显示消息?
【发布时间】:2013-03-07 21:16:38
【问题描述】:

我怎样才能只在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,上面的代码就认为它属于首页。

【问题讨论】:

  • 您使用的 joomla 版本是什么?
  • 您可以更好地了解如何查看主页docs.joomla.org/…

标签: php joomla


【解决方案1】:

这与链接中的“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,并且可能会更改主页菜单中定义的视图和选项,因此不会发生触发器。

【讨论】:

  • 不错的+1,只是开头的一个错字$jinput
  • 谢谢!修正了错字,意味着它们都是 $jinput,因为它通常在文档中是这样的。
【解决方案2】:

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

if (preg_match('/index\.php$/',$_SERVER['PHP_SELF'])) {
  echo "this is the homepage";
 }

【讨论】:

    【解决方案3】:

    你应该试试这个。 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-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      相关资源
      最近更新 更多