【问题标题】:Get ACF field from WordPress menu or sub-menu item从 WordPress 菜单或子菜单项获取 ACF 字段
【发布时间】:2020-06-21 03:31:26
【问题描述】:

我有一个菜单,我需要遍历 Wordpress 中父菜单项的所有子项,以获取作为子项一部分的 ACF 字段。

Menu parent 1 <-- ID is 95
  child of menu parent 1 <-- Get ACF field from this child
  child of menu parent 1 <-- Get ACF field from this child

如何获取子菜单项的 ACF 字段?

【问题讨论】:

    标签: php wordpress foreach advanced-custom-fields


    【解决方案1】:

    您可以像这样遍历菜单并检查该字段是否存在:

    add_filter('wp_nav_menu_objects', 'mlnc_wp_nav_menu_objects', 10, 2);
    
    function mlnc_wp_nav_menu_objects( $items, $args ) {
      // loop
      foreach( $items as $item ) {
        // vars
        $your_field = get_field('menu_placeholder', $item);
        // append field
        if( $your_field ) {
          $item->title .= ' <span>'.$your_field.'</span>';
        }
      }
      // return
      return $items;
    }
    

    在此处查看更多详细信息,the ACF reference for menus

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多