post_format_meta_box()
概述
post_format_meta_box() 函数用于在 WordPress 后台显示文章格式选择表单元素,适用于支持文章格式的主题和文章类型。它生成一个单选按钮列表,允许用户为文章选择格式。
关键要点
- 函数检查当前主题和文章类型是否支持文章格式,仅当两者都支持时才显示表单。
- 参数包括 $post(WP_Post 对象)和 $box(数组,包含元框的 id、title、callback 和 args)。
- 使用 get_theme_support() 获取主题支持的文章格式列表,并处理当前文章格式的显示逻辑。
- 输出 HTML 表单元素,包括隐藏输入和单选按钮,使用 checked() 函数设置选中状态。
代码示例
function post_format_meta_box( $post, $box ) {
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :
$post_formats = get_theme_support( 'post-formats' );
if ( is_array( $post_formats[0] ) ) :
$post_format = get_post_format( $post->ID );
if ( ! $post_format ) {
$post_format = '0';
}
if ( $post_format && ! in_array( $post_format, $post_formats[0], true ) ) {
$post_formats[0][] = $post_format;
}
?>
<!-- HTML output for post format selection -->
<?php
endif;
endif;
}注意事项
- 确保主题通过 add_theme_support('post-formats') 注册支持文章格式,否则函数不会输出内容。
- 文章类型必须通过 register_post_type() 或 add_post_type_support() 启用 post-formats 支持。
- 函数自 WordPress 3.1.0 版本引入,兼容性良好。
Displays post format form elements.
Parameters
$postWP_Postrequired-
Current post object.
$boxarrayrequired-
Post formats meta box arguments.
idstringMeta box'id'attribute.titlestringMeta box title.callbackcallableMeta box display callback.argsarrayExtra meta box arguments.
Source
function post_format_meta_box( $post, $box ) {
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :
$post_formats = get_theme_support( 'post-formats' );
if ( is_array( $post_formats[0] ) ) :
$post_format = get_post_format( $post->ID );
if ( ! $post_format ) {
$post_format = '0';
}
// Add in the current one if it isn't there yet, in case the active theme doesn't support it.
if ( $post_format && ! in_array( $post_format, $post_formats[0], true ) ) {
$post_formats[0][] = $post_format;
}
?>
<div id="post-formats-select">
<fieldset>
<legend class="screen-reader-text">
</legend>
<input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, '0' ); ?> /> <label for="post-format-0" class="post-format-icon post-format-standard"></label>
<br /><input type="radio" name="post_format" class="post-format" id="post-format-<?php echo esc_attr( $format ); ?>" value="<?php echo esc_attr( $format ); ?>" <?php checked( $post_format, $format ); ?> /> <label for="post-format-<?php echo esc_attr( $format ); ?>" class="post-format-icon post-format-<?php echo esc_attr( $format ); ?>"></label>
</fieldset>
</div>
</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#L518">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L518-L549">View on GitHub</a></p></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_theme_support/">get_theme_support()</a><code>wp-includes/theme.php
Gets the theme support arguments passed when registering that support.
checked()wp-includes/general-template.php
Outputs the HTML checked attribute.
post_type_supports()wp-includes/post.php
Checks a post type’s support for a given feature.
get_post_format()wp-includes/post-formats.php
Retrieve the format slug for a post
get_post_format_string()wp-includes/post-formats.php
Returns a pretty, translated version of a post format slug
current_theme_supports()wp-includes/theme.php
Checks a theme’s support for a given feature.
_e()wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
esc_html()wp-includes/formatting.php
Escaping for HTML blocks.
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |