get_post_field

函式
get_post_field ( $field, $post = null, $context = 'display' )
引數
  • (string) $field Post field name.
    Required:
  • (int|WP_Post) $post Optional. Post ID or post object. Defaults to global $post.
    Required:
    Default: null
  • (string) $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', or 'display'. Default 'display'.
    Required:
    Default: 'display'
返回值
  • (string) The value of the post field on success, empty string on failure.
相關
  • sanitize_post_field()
定義位置
相關方法
get_post_timeget_term_fieldget_post_typeget_post_metasanitize_post_field
引入
2.3.0
棄用
-

get_post_field函式是一個WordPress函式,用於檢索文章中特定欄位的值。它需要兩個引數:第一個是要檢索的欄位的名稱,例如“post_title”、“post_content”或“post_date”。第二個引數是要從中檢索欄位的文章的ID。如果沒有提供文章ID,該函式將從當前顯示的文章中檢索欄位。

根據文章ID從一個文章欄位中檢索資料。

文章欄位的例子有:”post_type”、”post_status”、”post_content”等,並基於文章物件的屬性或鍵名。

上下文的值是基於分類法的過濾功能,支援的值可以在這些功能中找到。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_post_field( $field, $post = null, $context = 'display' ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
if ( ! isset( $post->$field ) ) {
return '';
}
return sanitize_post_field( $field, $post->$field, $post->ID, $context );
}
function get_post_field( $field, $post = null, $context = 'display' ) { $post = get_post( $post ); if ( ! $post ) { return ''; } if ( ! isset( $post->$field ) ) { return ''; } return sanitize_post_field( $field, $post->$field, $post->ID, $context ); }
function get_post_field( $field, $post = null, $context = 'display' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	if ( ! isset( $post->$field ) ) {
		return '';
	}

	return sanitize_post_field( $field, $post->$field, $post->ID, $context );
}

常見問題

FAQs
檢視更多 >