0

I want to create a Joomla menu from scratch myself!
I studied a bit about the Joomla API, I know I should use Jmenu,but when I use:

   <?php $M=Jmenu::load();
    echo $M;?>

it returns some errors:

Notice: Array to string conversion in K:\xampp\htdocs\mag2\templates\mymagazine4\index.php on line 7 Array

or print nothing.

this is link Jmenu Jmenu -
How should I use it to show a menu???

thanks for reply

2

You have to get instance of menu:

$menu = JMenu::getInstance('site'); // or 'administrator' for back-end

this will load all items for you.

Now you can:

  1. Get all menu items

    $allMenuItems = $menu->getItems($attributes = array(), $values = array());

  2. Get default item (homepage)

    $defaultMenuItem = $menu->getDefault($language = '*');

To print your menu links, start with this:

$menu = JMenu::getInstance('site');
$allMenuItems = $menu->getItems($attributes = array(), $values = array());
if (count($allMenuItems)>0) {
  foreach ($allMenuItems as $menuItem) {
    echo '<a href="'.JRoute::_($menuItem->link).'">'.$menuItem->title.'</a>';
  }
} else {
  echo 'No menu items.';
}
  • when i use $menu=.... it is show any thing. – stackprogramer Feb 3 '16 at 8:40
  • when i use echo $menu,it has error Catchable fatal error: Object of class JMenuSite could not be converted to string in K:\xampp\htdocs\mag2\templates\mymagazine4\index.php on line 7 – stackprogramer Feb 3 '16 at 8:41
  • i write these code in html folder my template >mod_menu>index.php – stackprogramer Feb 3 '16 at 8:41
  • You can't echo PHP object. What are you trying to do? To get items, use $allMenuItems = $menu->getItems();print_r($allMenuItems); and you will see array of all menu items. Then you should iterate over eeach one and echo you links. – Rene Korss Feb 3 '16 at 8:42
  • 1
    I edited my code. Which these $attributes and $values you can basically get certain menu items (by language, access level) etc. – Rene Korss Feb 3 '16 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.