如何仅在 joomla 主页上显示消息?

2022-01-06 00:00:00 php joomla

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

How can I display a message only on the homepage of joomla? I am interested in displaying it on site.com and not site.com/index.php/page or anything else then site.com.

我已经测试了以下内容:

I have tested the following:

    <?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,上面的代码就认为是首页的.

The problem is that I can still see the message "this is the homepage" on pages like http://site.com/index.php/category/id/78-article which clearly isn't the homepage. It seems that whenever there is index.php in the link, the above code thinks it belongs to the homepage.

推荐答案

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

This has nothing to do with the 'index.php' in the link. Instead it is related to the fact that the link at http://site.com/index.php/category/id/78-article does not have a menu item associated with it. To do exactly what you are wanting, you will probably need to get a little trickier with the code and check to make sure that the actual page's information matches the homepage information:

$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 值.

I am checking the default menu items option (which component) and view and id values against those set in the input.

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

http://site.com/index.php/category/id/78-article This link will set the id to 78 and likely change the view and option from what it is defined as in the menu for the homepage, so the trigger will not occur.

相关文章