简体   繁体   English

从 WordPress 菜单或子菜单项中获取 ACF 字段

[英]Get ACF field from WordPress menu or sub-menu item

I have a menu where I need to loop through all the child items of a parent menu item in Wordpress to get an ACF field that's part of the child item.我有一个菜单,我需要在 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

How do I get the ACF fields of child menu items?如何获取子菜单项的 ACF 字段?

You can loop through menus and check if the field exists like this:您可以循环浏览菜单并检查该字段是否存在,如下所示:

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;
}

check for more details here, the ACF reference for menus在此处查看更多详细信息, 菜单的 ACF 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM