函数文档

page_attributes_meta_box()

💡 云策文档标注

概述

page_attributes_meta_box() 函数用于在 WordPress 后台编辑页面时,显示页面属性元框中的表单字段,包括父页面选择、模板选择和菜单顺序等。它主要处理分层文章类型(如页面)的属性设置。

关键要点

  • 函数接受一个 WP_Post 对象作为必需参数,表示当前编辑的页面。
  • 仅当文章类型是分层(hierarchical)时,才会显示父页面下拉菜单,使用 wp_dropdown_pages() 生成。
  • 支持页面模板选择,通过 page_template_dropdown() 显示可用模板,并应用 default_page_template_title 过滤器。
  • 如果文章类型支持 'page-attributes' 特性,会显示菜单顺序输入字段。
  • 包含多个 Hook:page_attributes_dropdown_pages_args 过滤器用于修改下拉菜单参数,page_attributes_meta_box_template 和 page_attributes_misc_attributes 动作用于扩展功能。
  • 函数内部使用条件判断,如检查是否为博客页面或是否有帮助标签,以优化显示内容。

代码示例

function page_attributes_meta_box( $post ) {
    if ( is_post_type_hierarchical( $post->post_type ) ) :
        $dropdown_args = array(
            'post_type'        => $post->post_type,
            'exclude_tree'     => $post->ID,
            'selected'         => $post->post_parent,
            'name'             => 'parent_id',
            'show_option_none' => __( '(no parent)' ),
            'sort_column'      => 'menu_order, post_title',
            'echo'             => 0,
        );
        $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
        $pages = wp_dropdown_pages( $dropdown_args );
        if ( ! empty( $pages ) ) :
            // 输出父页面选择 HTML
        endif;
    endif;
    // 其他代码(如模板选择、菜单顺序等)
}

注意事项

  • 此函数通常由 WordPress 核心在编辑页面时自动调用,开发者无需直接调用,但可通过 Hook 自定义其行为。
  • 确保文章类型正确配置为分层,以启用父页面选择功能。
  • 使用相关函数如 get_page_templates() 获取主题中的页面模板列表。

📄 原文内容

Displays page attributes form fields.

Parameters

$postWP_Postrequired
Current post object.

Source

function page_attributes_meta_box( $post ) {
if ( is_post_type_hierarchical( $post->post_type ) ) :
$dropdown_args = array(
'post_type' => $post->post_type,
'exclude_tree' => $post->ID,
'selected' => $post->post_parent,
'name' => 'parent_id',
'show_option_none' => __( '(no parent)' ),
'sort_column' => 'menu_order, post_title',
'echo' => 0,
);

/**
* Filters the arguments used to generate a Pages drop-down element.
*
* @since 3.3.0
*
* @see wp_dropdown_pages()
*
* @param array $dropdown_args Array of arguments used to generate the pages drop-down.
* @param WP_Post $post The current post.
*/
$dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
$pages = wp_dropdown_pages( $dropdown_args );
if ( ! empty( $pages ) ) :
?>
<p class="post-attributes-label-wrapper parent-id-label-wrapper"><label class="post-attributes-label" for="parent_id"></label></p>

0 && (int) get_option( 'page_for_posts' ) !== $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false;
?>
<p class="post-attributes-label-wrapper page-template-label-wrapper"><label class="post-attributes-label" for="page_template"></label>

</p>
<select name="page_template" id="page_template">

<option value="default"></option>
post_type ); ?>
</select>

post_type, 'page-attributes' ) ) : ?>
<p class="post-attributes-label-wrapper menu-order-label-wrapper"><label class="post-attributes-label" for="menu_order"></label></p>
<input name="menu_order" type="text" size="4" id="menu_order" value="<?php echo esc_attr( $post->menu_order ); ?>" />

post_type && get_current_screen()->get_help_tabs() ) : ?>
<p class="post-attributes-help-text"></p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/meta-boxes.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/meta-boxes.php#L1003">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L1003-L1087">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/default_page_template_title/"><span class="hook-func">apply_filters</span>( ‘default_page_template_title’, <nobr><span class="arg-type">string</span> <span class="arg-name">$label</span></nobr>, <nobr><span class="arg-type">string</span> <span class="arg-name">$context</span></nobr> )</a></dt><dd><p>Filters the title of the default page template displayed in the drop-down.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/page_attributes_dropdown_pages_args/"><span class="hook-func">apply_filters</span>( ‘page_attributes_dropdown_pages_args’, <nobr><span class="arg-type">array</span> <span class="arg-name">$dropdown_args</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the arguments used to generate a Pages drop-down element.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/page_attributes_meta_box_template/"><span class="hook-func">do_action</span>( ‘page_attributes_meta_box_template’, <nobr><span class="arg-type">string|false</span> <span class="arg-name">$template</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires immediately after the label inside the ‘Template’ section of the ‘Page Attributes’ meta box.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/page_attributes_misc_attributes/"><span class="hook-func">do_action</span>( ‘page_attributes_misc_attributes’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires before the help hint text in the ‘Page Attributes’ meta box.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_page_templates/">get_page_templates()</a><code>wp-admin/includes/theme.php

Gets the page templates available in this theme.

get_current_screen()wp-admin/includes/screen.php

Get the current screen object

page_template_dropdown()wp-admin/includes/template.php

Prints out option HTML elements for the page templates drop-down.

wp_dropdown_pages()wp-includes/post-template.php

Retrieves or displays a list of pages as a dropdown (select list).

post_type_supports()wp-includes/post.php

Checks a post type’s support for a given feature.

is_post_type_hierarchical()wp-includes/post.php

Determines whether the post type is hierarchical.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

esc_html()wp-includes/formatting.php

Escaping for HTML blocks.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

apply_filters()wp-includes/plugin.php

Calls the callback functions that have been added to a filter hook.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

get_option()wp-includes/option.php

Retrieves an option value based on an option name.

Show 8 moreShow less

Changelog

Version Description
2.7.0 Introduced.