【问题标题】:Getting Menu Parameters from Joomla从 Joomla 获取菜单参数
【发布时间】:2011-02-11 18:45:59
【问题描述】:

我正在尝试从 Joomla 的菜单表中获取参数。我下面的内容在返回参数的意义上是有效的。

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

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

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

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

【问题讨论】:

    标签: php joomla


    【解决方案1】:

    我认为 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');
    

    或者先检查是否填写,如果是,echoit:

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

    【讨论】:

    • 关于if ($menu_image &amp;&amp; strlen($menu_image) { ...strlen($menu_image)是冗余检查;第一次真实检查将完成这项工作。您还需要一个右括号。
    【解决方案2】:

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

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

    【讨论】:

    • 这在编写时是正确的答案,但从一些 Joomla 2.5 补丁开始,JParameter 类已被弃用。代替 JParameter 类,以相同的方式使用 JRegistry 类: $params = new JRegistry($item->params);
    【解决方案3】:

    没用

    尝试使用这个:

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

    【讨论】:

    • 这是哪个 Joomla 版本?看起来很旧。
    【解决方案4】:
    $app = JFactory::getApplication();
    $params = $app->getParams();
    $yourParameter = $params->get('YOURPARAMETERNAME');
    

    【讨论】:

      【解决方案5】:
       ($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
          })
      

      【讨论】:

        【解决方案6】:

        在 3.5.1 中工作

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

        显示菜单项图像

        【讨论】:

          【解决方案7】:

          JParameter 在 Joomla 2.5 中已弃用,因此要让 Kevin 的代码正常工作,请添加

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

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

          【讨论】:

          • 这不是一个真正的解决方案,更像是重新包含已弃用的类和函数的 hack ;)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-01
          • 1970-01-01
          • 2012-04-26
          • 2013-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多