name = 'nav_menu'; $this->label = __( 'Nav Menu' ); $this->category = __( 'Relational' ); // Basic, Content, Choice, etc $this->defaults = array( 'save_format' => 'id', 'allow_null' => 0, 'container' => 'div', ); parent::__construct(); } /** * Renders the Nav Menu Field options seen when editing a Nav Menu Field. * * @param array $field The array representation of the current Nav Menu Field. */ public function create_options( $field ) { $field = array_merge( $this->defaults, $field ); $key = $field['name']; // Create Field Options HTML ?> 'radio', 'name' => 'fields['.$key.'][save_format]', 'value' => $field['save_format'], 'layout' => 'horizontal', 'choices' => array( 'object' => __( 'Nav Menu Object' ), 'menu' => __( 'Nav Menu HTML' ), 'id' => __( 'Nav Menu ID' ), ), ) ); ?>

'select', 'name' => 'fields['.$key.'][container]', 'value' => $field['container'], 'choices' => $this->get_allowed_nav_container_tags(), ) ); ?> 'radio', 'name' => 'fields['.$key.'][allow_null]', 'value' => $field['allow_null'], 'layout' => 'horizontal', 'choices' => array( 1 => __( 'Yes' ), 0 => __( 'No' ), ), ) ); ?> get_nav_menus( $allow_null ); if ( empty( $nav_menus ) ) { return; } ?> false ) ); $nav_menus = array(); if ( $allow_null ) { $nav_menus[''] = ' - Select - '; } foreach ( $navs as $nav ) { $nav_menus[ $nav->term_id ] = $nav->name; } return $nav_menus; } /** * Get the allowed wrapper tags for use with wp_nav_menu(). * * @return array An array of allowed wrapper tags. */ private function get_allowed_nav_container_tags() { $tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); $formatted_tags = array( '0' => 'None', ); foreach ( $tags as $tag ) { $formatted_tags[$tag] = ucfirst( $tag ); } return $formatted_tags; } /** * Renders the Nav Menu Field. * * @param int $value The Nav Menu ID selected for this Nav Menu Field. * @param int $post_id The Post ID this $value is associated with. * @param array $field The array representation of the current Nav Menu Field. * * @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false. */ public function format_value_for_api( $value, $post_id, $field ) { $field = array_merge($this->defaults, $field); if( empty( $value ) ) { return false; } // check format if( 'object' == $field['save_format'] ) { $wp_menu_object = wp_get_nav_menu_object( $value ); if( empty( $wp_menu_object ) ) { return false; } $menu_object = new stdClass; $menu_object->ID = $wp_menu_object->term_id; $menu_object->name = $wp_menu_object->name; $menu_object->slug = $wp_menu_object->slug; $menu_object->count = $wp_menu_object->count; return $menu_object; } elseif( 'menu' == $field['save_format'] ) { ob_start(); wp_nav_menu( array( 'menu' => $value, 'container' => $field['container'] ) ); return ob_get_clean(); } // Just return the Nav Menu ID return $value; } } new ACF_Field_Nav_Menu_V4();