返回介绍

wp_setup_nav_menu_item()

发布于 2017-09-11 12:49:41 字数 10599 浏览 801 评论 0 收藏 0

wp_setup_nav_menu_item( object $menu_item )

Decorates a menu item object with the shared navigation menu item properties.


description

Properties:

  • ID: The term_id if the menu item represents a taxonomy term.
  • attr_title: The title attribute of the link element for this menu item.
  • classes: The array of class attribute values for the link element of this menu item.
  • db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn’t exist).
  • description: The description of this menu item.
  • menu_item_parent: The DB ID of the nav_menu_item that is this item’s menu parent, if any. 0 otherwise.
  • object: The type of object originally represented, such as "category," "post", or "attachment."
  • object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
  • post_parent: The DB ID of the original object’s parent object, if any (0 otherwise).
  • post_title: A "no title" label if menu item represents a post that lacks a title.
  • target: The target attribute of the link element for this menu item.
  • title: The title of this menu item.
  • type: The family of objects originally represented, such as "post_type" or "taxonomy."
  • type_label: The singular label used to describe this type of menu item.
  • url: The URL to which this menu item points.
  • xfn: The XFN relationship expressed in the link of this menu item.
  • _invalid: Whether the menu item represents an object that no longer exists.

参数

$menu_item

(object) (Required) The menu item to modify.


返回值

(object) $menu_item The menu item with standard menu item properties.


源代码

File: wp-includes/nav-menu.php

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

				$menu_item->url = get_permalink( $menu_item->object_id );

				$original_object = get_post( $menu_item->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 );

				if ( '' === $original_title ) {
					/* translators: %d: ID of a post */
					$original_title = sprintf( __( '#%d (no title)' ), $original_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 {
					$menu_item->_invalid = true;
					$post_type_description = '';
				}

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

				$term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
				$menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';

				$original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
				if ( is_wp_error( $original_title ) )
					$original_title = false;
				$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 );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_trim_words()
  • wp-includes/taxonomy.php: get_term_link()
  • wp-includes/taxonomy.php: get_term_field()
  • wp-includes/taxonomy.php: get_taxonomy()
  • wp-includes/link-template.php: get_post_type_archive_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: the_title
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: get_post_type_object()
  • wp-includes/post.php: get_post_status()
  • wp-includes/post.php: get_post()
  • wp-includes/nav-menu.php: nav_menu_attr_title
  • wp-includes/nav-menu.php: nav_menu_description
  • wp-includes/nav-menu.php: wp_setup_nav_menu_item
  • wp-includes/load.php: is_wp_error()
  • Show 12 more uses Hide more uses

Used By

  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::value()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_menu_item()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文