Blog

How to detect if we are browsing the home page in Joomla 4

 Below you can find a PHP snippet that will allow you to detect whether we are browsing the home page. 

 

use Joomla\CMS\Factory;

//check homepage
$app = Factory::getApplication();
$isHomepage = $app->getMenu()->getActive()->home;
if($isHomepage){
echo 'home';	
}else{
echo 'no';	
}

How to Get Menu in Joomla

In Joomla a Menu is the set of navigation links you can have as a main menu (at the top of site) or side menu or footer menu. Each of the individual navigation link is a Menu item. You can easily get menu items in Joomla.

To get the menu:

use Joomla\CMS\Factory;
$app = Factory::getApplication();
$menu = $app->getMenu();

Properties

The variable $items is an array of menu item objects. It contains the following:

  • id
  • menutype
  • title
  • alias
  • note
  • route
  • link
  • type
  • level
  • language
  • browserNav
  • access
  • home
  • template_style_id
  • component_id
  • parent_id
  • component
  • tree (Array)
  • query (Array)

All the other attributes of the menu item are stored in the params field in the database and accessed using the getParams() method.

$params = $menuitem->getParams(); 
$displayed = $params->get("menu_show");
$show_tags = $params->get("show_tags");