返回介绍

wp_get_associated_nav_menu_items()

发布于 2017-09-11 11:58:45 字数 2773 浏览 1117 评论 0 收藏 0

wp_get_associated_nav_menu_items( int $object_id,  string $object_type = 'post_type',  string $taxonomy = '' )

Get the menu items associated with a particular object.


description


参数

$object_id

(int) (Required) The ID of the original object.

$object_type

(string) (Optional) The type of object, such as "taxonomy" or "post_type."

Default value: 'post_type'

$taxonomy

(string) (Optional) If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to

Default value: ''


返回值

(array) The array of menu item IDs; empty array if none;


源代码

File: wp-includes/nav-menu.php

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

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-query.php: WP_Query::__construct()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/nav-menu.php: is_nav_menu_item()

Used By

  • wp-includes/nav-menu.php: _wp_delete_post_menu_item()
  • wp-includes/nav-menu.php: _wp_delete_tax_menu_item()

User Contributed Notes

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

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

发布评论

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