从Joomla获取菜单参数

时间:2011-02-11 18:45:59

标签: php joomla

我正在尝试从Joomla的菜单表中获取参数。我在下面的工作是在返回参数。

 $menu =   &JSite::getMenu();
 $item =  $menu->getItem($menuId)->params;
 print $items;

但是,它以纯文本形式返回它们,好像我刚刚查询了列并返回了params内容。

有人可以告诉我如何将其作为对象或数组返回,以便我可以使用类似的东西:

$myParam = $item->getParams('theParamIwant');

7 个答案:

答案 0 :(得分:12)

我认为JParameter在Joomla已经过时了! 3.x,所以答案现在就像:

 $app = JFactory::getApplication();
 $menuitem   = $app->getMenu()->getActive(); // get the active item
 $menuitem   = $app->getMenu()->getItem($theid); // or get item by ID
 $params = $menuitem->params; // get the params
 print_r($params); // print all params as overview

您可以通过执行以下操作获取menu_image变量:

 echo $params->get('menu_image');

或首先检查它是否已填写,如果是,echo

// using get() with a second parameter makes it fall back to this if nothing is found
$menu_image = $params->get('menu_image', false);
if ($menu_image && strlen($menu_image) {
   echo "<img src='$menu_image'/>";
}

或者,使用tertiary运算符:

$menuimg = $params->get('menu_image')
echo strlen($menuimg) ? "<img src='$menuimg'/>" : '';

答案 1 :(得分:7)

您需要使用JParameter类来读取参数。尝试这样的事情:

$item = $menu->getItem($menuId);
$params = new JParameter($item->params);
$myParam = $params->get('theParamIwant');

答案 2 :(得分:2)

不起作用

尝试使用它:

$params = $menus->getParams($menuId);
$myParam = $params->get('theParamIwant');

答案 3 :(得分:0)

$app = JFactory::getApplication();
$params = $app->getParams();
$yourParameter = $params->get('YOURPARAMETERNAME');

答案 4 :(得分:0)

 ($currentMenuId = JSite::getMenu()->getActive()->id ; // `enter code here`
    $document =& JFactory::getDocument(); // `enter code here`
    $app = JFactory::getApplication(); // `enter code here`
    $menuitem   = $app->getMenu()->getItem($currentMenuId); // or get item by ID `enter code here`
    $params = $menuitem->params; // get the params `enter code here`
    $params->get('menu-meta_keywords');
    if($document->description == '') // 116 is the ID number of the menu pointing to the component `enter code here`
    {
    $this->setMetaData( 'description', $params->get('menu-meta_description') );
    $this->setMetaData( 'keywords', $params->get('menu-meta_keywords') );
    }
    else
    {
    // do nothing
    })

答案 5 :(得分:0)

使用3.5.1

$app = JFactory::getApplication();
$currentMenuId = JSite::getMenu()->getActive()->id;
$menuitem   = $app->getMenu()->getItem($currentMenuId);
$params = $menuitem->params;
echo $params['menu_image'];

显示菜单项图像

答案 6 :(得分:-1)

在Joomla 2.5中不推荐使用JParameter,因此要使Kevin的代码能够正常工作

之前

jimport( 'joomla.html.parameter' )

jimport( 'joomla.html.parameter' );
$item = $menu->getItem($menuId);
$params = new JParameter($item->params);
$myParam = $params->get('theParamIwant');