php WordPress |获取现场所有菜单,添加到选择字段。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WordPress |获取现场所有菜单,添加到选择字段。相关的知识,希望对你有一定的参考价值。

// Get registered menus, populate select field
function acf_load_menu_field_choices( $field ) {
// reset choices
$field['choices'] = array();
$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
//$menus = get_registered_nav_menus(); //uncomment this if you want to populate the dropdown with all Menu Locations
$blank_list = json_encode(array( "name" => "Primary Navigation", "term_id" => ""));
$blank_list = json_decode($blank_list);
array_unshift($menus, $blank_list);

foreach ( $menus as $val ) {
  $value = $val->term_id;
  $label = $val->name;
  $field['choices'][ $value ] = $label;
}

// return the field
return $field;

}
add_filter('acf/load_field/name=menu_select', __NAMESPACE__ . '\\acf_load_menu_field_choices'); //replace custom_menu with your field name
// Menu Select
public function menu_select()
{
  $menu = get_field('menu_select');

  if(!empty($menu)) {
    $menu = strtolower( str_replace(' ', '-', $menu['label']) );
  }

  return $menu;
}
@if (!empty($menu_select))
<div class="w-100 secondary-nav {!! $mobile_breakpoint !!}">
  <div class="container px-5">
    <nav>
      {!! wp_nav_menu([
        'menu'              => $menu_select,
        'depth'             => 2,
        'container'         => 'div',
        'container_class'   => 'navbar-collapse',
        'container_id'      => 'bs-example-navbar-collapse-1',
        'menu_class'        => 'position-relative m-0 px-0 py-3 list-unstyled d-flex',
      ]) !!}
    </nav>
  </div>
</div>
@endif

以上是关于php WordPress |获取现场所有菜单,添加到选择字段。的主要内容,如果未能解决你的问题,请参考以下文章

php 使用自定义帖子类型获取自定义WordPress菜单

WordPress主题开发:开启导航菜单功能

用于Wordpress的菜单和子菜单PHP

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

wordpress文章页两侧添加分页导航箭头

WordPress/PHP 获取主导航中所有链接的列表