get_post_field()
get_post_field( string $field, int|WP_Post $post = null…
get_post_field( string $field, int|WP_Post $post = null, string $context = ‘display’ )
基于post ID从post字段检索数据。
Retrieve data from a post field based on Post ID.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
另见函数 sanitize_post_field()
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $field | (string) | 必需 | Post字段名。 |
| $post | (int | WP_Post) | 可选 | Post ID或Post对象。默认为全局$post。 |
| $context | (string) | 可选 | 如何过滤字段。接受“raw”、“edit”、“db”或“display”。 |
返回(Return)
(string)成功时post字段的值,失败时为空string。
源码(Source)
/**
* Retrieve data from a post field based on Post ID.
*
* Examples of the post field will be, 'post_type', 'post_status', 'post_content',
* etc and based off of the post object property or key names.
*
* The context values are based off of the taxonomy filter functions and
* supported values are found within those functions.
*
* @since 2.3.0
*
* @see sanitize_post_field()
*
* @param string $field Post field name.
* @param int|WP_Post $post Post ID or post object.
* @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
* or 'display'. Default 'display'.
* @return string The value of the post field on success, empty string on failure.
*/
function get_post_field( $field, $post, $context = 'display' ) {
$post = get_post( $post );
if ( !$post )
return '';
if ( !isset($post->$field) )
return '';
return sanitize_post_field($field, $post->$field, $post->ID, $context);
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 4.5.0 | wp-includes/post.php:849 | 3 | 2 |
笔记(Notes)
以下是您可以获取的默认post字段(区分大小写):
$field参数区分大小写。例子:
在循环外获取帖子的作者:
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!