簡體   English   中英

從 Joomla 獲取菜單參數

[英]Getting Menu Parameters from Joomla

我正在嘗試從 Joomla 的菜單表中獲取參數。 我在下面的工作是在返回參數的意義上。

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

但是,它以純文本形式返回它們,就好像我剛剛查詢了該列並返回了 params 內容。

有人可以告訴我如何將其作為對象或數組返回,以便我可以使用以下內容:

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

我認為 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'/>" : '';

您需要使用 JParameter 類來讀取參數。 嘗試這樣的事情:

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

它不起作用

嘗試使用這個:

$params = $menus->getParams($menuId);
$myParam = $params->get('theParamIwant');
$app = JFactory::getApplication();
$params = $app->getParams();
$yourParameter = $params->get('YOURPARAMETERNAME');
 ($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
    })

在 3.5.1 中工作

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

顯示菜單項圖像

JParameter 在 Joomla 2.5 中被棄用,所以為了讓 Kevin 的代碼工作添加

jimport( 'joomla.html.parameter' )在 ie 之前

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM