2011-02-11 89 views
4

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

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

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

谁能告诉我如何返回这是一个对象或数组,这样我可以使用类似:

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

回答

6

您需要使用JParameter类阅读PARAMS。尝试是这样的:

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

感谢凯文,你达人.. – tpow 2011-02-11 20:19:51

+2

这是正确的anwser它被写入时,但从一些Joomla 2.5补丁开始,JParameter类已被弃用。而不是JParameter类以相同的方式使用JRegistry类:$ params = new JRegistry($ item-> params); – 2014-12-23 09:01:48

2

它不工作

尝试使用此:

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

没有兄弟,Kevin的答案确实有效...... – tpow 2012-01-24 14:49:46

-1

JParameter被弃用的Joomla 2.5,所以要得到凯文的代码工作添加

jimport('joomla.html.parameter')之前即

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

我认为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'/>" : ''; 
0
$app = JFactory::getApplication(); 
$params = $app->getParams(); 
$yourParameter = $params->get('YOURPARAMETERNAME'); 
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 
    }) 
0

3.5.1

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

工作显示菜单项图像