WordPress函数get_post_field()获取文章字段信息

描述:

获取文章字段信息

用法:

<?php get_post_field( $field, $post_id, $context ); ?>

参数:

$field

(string) (必填) 文章字段名称

默认值: None

*_posts表中的任何字段::

  • post_content
  • post_author
  • post_title
  • post_name (=slug)
  • etc.

$post_id

(id) (必填) 文章ID

默认值: None

$context

(string) (可选)  如何过滤字段。可能的上下文值是:“raw”、“edit”、“db”、“display”、“attribute”和“js”。默认情况下使用“display”上下文在调用筛选器时,属性“和”js“上下文被视为”display“。

默认值: display

源文件:

/**
 * 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);
}