钩子文档

nav_menu_items_{$post_type_name}

💡 云策文档标注

概述

nav_menu_items_{$post_type_name} 是一个 WordPress 过滤器钩子,用于控制当前文章类型菜单项元框中“查看全部”标签页显示的帖子列表。它允许开发者基于文章类型动态过滤帖子数据。

关键要点

  • 钩子名称动态部分 $post_type_name 对应当前文章类型的 slug,例如 nav_menu_items_post 或 nav_menu_items_page。
  • 参数包括 $posts(帖子数组,可能包含 WP_Post 对象或“伪”帖子对象)、$args(WP_Query 参数数组)和 $post_type(当前文章类型对象)。
  • 从 WordPress 4.6.0 起,$post_type 参数接受 WP_Post_Type 对象,增强了类型安全性。

代码示例

$posts = apply_filters(
    "nav_menu_items_{$post_type_name}",
    $posts,
    $args,
    $post_type
);

注意事项

  • 此钩子主要用于 wp_nav_menu_item_post_type_meta_box() 函数中,影响后台菜单管理界面。
  • 开发者应确保过滤逻辑兼容不同文章类型,避免破坏菜单项的正常显示。

📄 原文内容

Filters the posts displayed in the ‘View All’ tab of the current post type’s menu items meta box.

Description

The dynamic portion of the hook name, $post_type_name, refers to the slug of the current post type.

Possible hook names include:

  • nav_menu_items_post
  • nav_menu_items_page

See also

Parameters

$postsobject[]
The posts for the current post type. Mostly WP_Post objects, but can also contain “fake” post objects to represent other menu items.
$argsarray
An array of WP_Query arguments.
$post_typeWP_Post_Type
The current post type object for this menu item meta box.

Source

$posts = apply_filters(
	"nav_menu_items_{$post_type_name}",
	$posts,
	$args,
	$post_type
);

Changelog

Version Description
4.6.0 Converted the $post_type parameter to accept a WP_Post_Type object.
3.2.0 Introduced.