list_meta()
概述
list_meta() 函数用于在自定义字段元框中输出文章的公共元数据。它接收一个元数据数组作为参数,并生成一个包含名称和值的表格。
关键要点
- 函数 list_meta( $meta ) 输出文章的公共元数据,参数 $meta 是一个必需的数组,键为 'meta_key' 和 'meta_value'。
- 如果 $meta 为空,函数会输出一个空的表格头并返回,否则会遍历数组并调用 list_meta_row() 输出每一行数据。
- 该函数主要用于 post_custom_meta_box() 中,用于显示自定义字段表单。
代码示例
function list_meta( $meta ) {
// Exit if no meta.
if ( ! $meta ) {
echo '<table id="list-table">
<thead>
<tr>
<th class="left">' . _x( 'Name', 'meta name' ) . '</th>
<th>' . __( 'Value' ) . '</th>
</tr>
</thead>
<tbody id="the-list" data-wp-lists="list:meta"></tbody>
</table>'; // TBODY needed for list-manipulation JS.
return;
}
$count = 0;
?>
<table id="list-table">
<thead>
<tr>
<th class="left"><?php _ex( 'Name', 'meta name' ); ?></th>
<th><?php _e( 'Value' ); ?></th>
</tr>
</thead>
<tbody id="the-list" data-wp-lists="list:meta">
<?php
foreach ( $meta as $entry ) {
list_meta_row( $entry, $count );
$count++;
}
?>
</tbody>
</table>
<?php
}注意事项
- 函数依赖于 list_meta_row() 来输出单行元数据,并使用了国际化函数如 _x()、__() 和 _e() 来显示翻译后的文本。
- 表格的 tbody 元素包含 data-wp-lists="list:meta" 属性,用于支持 JavaScript 列表操作。
- 该函数自 WordPress 1.2.0 版本引入,主要用于后台管理界面。
Outputs a post’s public meta data in the Custom Fields meta box.
Parameters
$metaarray[]required-
An array of meta data arrays keyed on
'meta_key'and'meta_value'.
Source
function list_meta( $meta ) {
// Exit if no meta.
if ( ! $meta ) {
echo '
<table id="list-table" style="display: none;">
<thead>
<tr>
<th class="left">' . _x( 'Name', 'meta name' ) . '</th>
<th>' . __( 'Value' ) . '</th>
</tr>
</thead>
<tbody id="the-list" data-wp-lists="list:meta">
<tr><td></td></tr>
</tbody>
</table>'; // TBODY needed for list-manipulation JS.
return;
}
$count = 0;
?>
<table id="list-table">
<thead>
<tr>
<th class="left"></th>
<th></th>
</tr>
</thead>
<tbody id='the-list' data-wp-lists='list:meta'>
</tbody>
</table>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/template.php#L585">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/template.php#L585-L620">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/_list_meta_row/">_list_meta_row()</a><code>wp-admin/includes/template.php
Outputs a single row of public meta data in the Custom Fields meta box.
_ex()wp-includes/l10n.php
Displays translated string with gettext context.
_x()wp-includes/l10n.php
Retrieves translated string with gettext context.
__()wp-includes/l10n.php
Retrieves the translation of $text.
_e()wp-includes/l10n.php
Displays translated text.
| Used by | Description |
|---|---|
post_custom_meta_box()wp-admin/includes/meta-boxes.php |
Displays custom fields form fields. |
Changelog
| Version | Description |
|---|---|
| 1.2.0 | Introduced. |