wp_setup_nav_menu_item

函数
wp_setup_nav_menu_item ( $menu_item )
参数
  • (object) $menu_item The menu item to modify.
    Required:
返回值
  • (object) The menu item with standard menu item properties.
定义位置
相关方法
wp_get_nav_menu_itemswp_save_nav_menu_itemswp_update_nav_menu_item_sort_nav_menu_itemsis_nav_menu_item
引入
3.0.0
弃用
-

wp_setup_nav_menu_item: 这个函数设置了一个导航菜单项。它接受一个菜单项对象,并修改它以包括额外的信息,如菜单项的URL。

用共享的导航菜单项属性来装饰一个菜单项对象。

属性:
– ID: 如果该菜单项代表一个分类术语,则是术语_ID。
– attr_title: 这个菜单项的链接元素的标题属性。
– classes: 这个菜单项的链接元素的类属性值的数组。
– db_id: 这个项目作为nav_menu_item对象的DB ID,如果它存在的话(如果它不存在的话是0)。
– description: 这个菜单项的描述。
– menu_item_parent: 如果有的话,作为这个项目的菜单父对象的nav_menu_item的DB ID。否则为0。
– object: 最初代表的对象的类型,如”类别”、”文章”或”附件”。
– object_id: 这个菜单项所代表的原始对象的DB ID,例如,文章的ID和类别的term_id。
– post_parent:原始对象的父对象的DB ID,如果有的话(否则为0)。
– post_title: 一个”无标题”的标签,如果菜单项代表一个缺乏标题的文章。
– target(目标): 这个菜单项的链接元素的目标属性。
– title: 这个菜单项的标题。
– type(类型): 最初代表的对象系列,如’post_type’或’taxonomy’。
– type_label: 用来描述这个类型的菜单项的单数标签。
– url: 这个菜单项所指向的URL。
– xfn:这个菜单项的链接中表达的XFN关系。
– _invalid(无效): 该菜单项是否代表一个不再存在的对象。

function wp_setup_nav_menu_item( $menu_item ) {
	if ( isset( $menu_item->post_type ) ) {
		if ( 'nav_menu_item' === $menu_item->post_type ) {
			$menu_item->db_id            = (int) $menu_item->ID;
			$menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
			$menu_item->object_id        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
			$menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
			$menu_item->type             = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;

			if ( 'post_type' === $menu_item->type ) {
				$object = get_post_type_object( $menu_item->object );
				if ( $object ) {
					$menu_item->type_label = $object->labels->singular_name;
					// Denote post states for special pages (only in the admin).
					if ( function_exists( 'get_post_states' ) ) {
						$menu_post   = get_post( $menu_item->object_id );
						$post_states = get_post_states( $menu_post );
						if ( $post_states ) {
							$menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
						}
					}
				} else {
					$menu_item->type_label = $menu_item->object;
					$menu_item->_invalid   = true;
				}

				if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
					$menu_item->_invalid = true;
				}

				$original_object = get_post( $menu_item->object_id );

				if ( $original_object ) {
					$menu_item->url = get_permalink( $original_object->ID );
					/** This filter is documented in wp-includes/post-template.php */
					$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
				} else {
					$menu_item->url      = '';
					$original_title      = '';
					$menu_item->_invalid = true;
				}

				if ( '' === $original_title ) {
					/* translators: %d: ID of a post. */
					$original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
				}

				$menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;

			} elseif ( 'post_type_archive' === $menu_item->type ) {
				$object = get_post_type_object( $menu_item->object );
				if ( $object ) {
					$menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
					$post_type_description = $object->description;
				} else {
					$post_type_description = '';
					$menu_item->_invalid   = true;
				}

				$menu_item->type_label = __( 'Post Type Archive' );
				$post_content          = wp_trim_words( $menu_item->post_content, 200 );
				$post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
				$menu_item->url        = get_post_type_archive_link( $menu_item->object );

			} elseif ( 'taxonomy' === $menu_item->type ) {
				$object = get_taxonomy( $menu_item->object );
				if ( $object ) {
					$menu_item->type_label = $object->labels->singular_name;
				} else {
					$menu_item->type_label = $menu_item->object;
					$menu_item->_invalid   = true;
				}

				$original_object = get_term( (int) $menu_item->object_id, $menu_item->object );

				if ( $original_object && ! is_wp_error( $original_object ) ) {
					$menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
					$original_title = $original_object->name;
				} else {
					$menu_item->url      = '';
					$original_title      = '';
					$menu_item->_invalid = true;
				}

				if ( '' === $original_title ) {
					/* translators: %d: ID of a term. */
					$original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
				}

				$menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;

			} else {
				$menu_item->type_label = __( 'Custom Link' );
				$menu_item->title      = $menu_item->post_title;
				$menu_item->url        = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
			}

			$menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;

			/**
			 * Filters a navigation menu item's title attribute.
			 *
			 * @since 3.0.0
			 *
			 * @param string $item_title The menu item title attribute.
			 */
			$menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;

			if ( ! isset( $menu_item->description ) ) {
				/**
				 * Filters a navigation menu item's description.
				 *
				 * @since 3.0.0
				 *
				 * @param string $description The menu item description.
				 */
				$menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
			}

			$menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
			$menu_item->xfn     = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
		} else {
			$menu_item->db_id            = 0;
			$menu_item->menu_item_parent = 0;
			$menu_item->object_id        = (int) $menu_item->ID;
			$menu_item->type             = 'post_type';

			$object                = get_post_type_object( $menu_item->post_type );
			$menu_item->object     = $object->name;
			$menu_item->type_label = $object->labels->singular_name;

			if ( '' === $menu_item->post_title ) {
				/* translators: %d: ID of a post. */
				$menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
			}

			$menu_item->title  = $menu_item->post_title;
			$menu_item->url    = get_permalink( $menu_item->ID );
			$menu_item->target = '';

			/** This filter is documented in wp-includes/nav-menu.php */
			$menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );

			/** This filter is documented in wp-includes/nav-menu.php */
			$menu_item->description = apply_filters( 'nav_menu_description', '' );
			$menu_item->classes     = array();
			$menu_item->xfn         = '';
		}
	} elseif ( isset( $menu_item->taxonomy ) ) {
		$menu_item->ID               = $menu_item->term_id;
		$menu_item->db_id            = 0;
		$menu_item->menu_item_parent = 0;
		$menu_item->object_id        = (int) $menu_item->term_id;
		$menu_item->post_parent      = (int) $menu_item->parent;
		$menu_item->type             = 'taxonomy';

		$object                = get_taxonomy( $menu_item->taxonomy );
		$menu_item->object     = $object->name;
		$menu_item->type_label = $object->labels->singular_name;

		$menu_item->title       = $menu_item->name;
		$menu_item->url         = get_term_link( $menu_item, $menu_item->taxonomy );
		$menu_item->target      = '';
		$menu_item->attr_title  = '';
		$menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
		$menu_item->classes     = array();
		$menu_item->xfn         = '';

	}

	/**
	 * Filters a navigation menu item object.
	 *
	 * @since 3.0.0
	 *
	 * @param object $menu_item The menu item object.
	 */
	return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
}

常见问题

FAQs
查看更多 >