How to display a message on joomla homepage only?

2019-07-21 10:34发布

问题:

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; ?>

and this

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

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.

回答1:

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";
}

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



回答2:

OK, just an idea but try:

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


回答3:

You should try this. 1. Check the menu in which has been assigned "Home"(This is the homepage) from the administrator section. Copy the link shown over there. It would be something like

index.php?option=com_somecomponent&view=someview
  1. Now Goto your index.php in template.
  2. Write a condition there

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

This should do the trick for you !! But keep in mind. If you change the menu of homepage, this will have to be changed accordingly.



标签: php joomla