wp_get_associated_nav_menu_items

函数
wp_get_associated_nav_menu_items ( $object_id = 0, $object_type = 'post_type', $taxonomy = '' )
参数
  • (int) $object_id Optional. The ID of the original object. Default 0.
    Required:
  • (string) $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'. Default 'post_type'.
    Required:
    Default: 'post_type'
  • (string) $taxonomy Optional. If $object_type is 'taxonomy', $taxonomy is the name of the tax that $object_id belongs to. Default empty.
    Required:
    Default: (empty)
返回值
  • (int[]) The array of menu item IDs; empty array if none.
定义位置
相关方法
wp_get_nav_menu_itemswp_save_nav_menu_items_sort_nav_menu_itemswp_update_nav_menu_itemwp_setup_nav_menu_item
引入
3.0.0
弃用
-

wp_get_associated_nav_menu_items: 这个函数返回一个与特定对象(如文章或术语)相关的菜单项的数组。

返回与一个特定对象相关的菜单项。

function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
	$object_id     = (int) $object_id;
	$menu_item_ids = array();

	$query      = new WP_Query;
	$menu_items = $query->query(
		array(
			'meta_key'       => '_menu_item_object_id',
			'meta_value'     => $object_id,
			'post_status'    => 'any',
			'post_type'      => 'nav_menu_item',
			'posts_per_page' => -1,
		)
	);
	foreach ( (array) $menu_items as $menu_item ) {
		if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
			$menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
			if (
				'post_type' === $object_type &&
				'post_type' === $menu_item_type
			) {
				$menu_item_ids[] = (int) $menu_item->ID;
			} elseif (
				'taxonomy' === $object_type &&
				'taxonomy' === $menu_item_type &&
				get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
			) {
				$menu_item_ids[] = (int) $menu_item->ID;
			}
		}
	}

	return array_unique( $menu_item_ids );
}

常见问题

FAQs
查看更多 >