WordPress函数:get_post_field 获取文章字段

追格官方小助手/ 2023年02月06日/ WordPress/ 浏览 2020

WordPress函数 get_post_field 是用来获取文章的字段的。


函数源码:


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


从源码上看,这个函数也是先用 get_post 函数查询文章,然后读取相应的字段。相对于直接读取,这个采用的是 sanitize_post_field 函数。


从源码上也可以看出,get_post_field 的 $field 是区分大小写的。


get_post_field('ID'); // 返回当前文章的 ID
get_post_field('id'); // 返回空字符串


$post的全部字段都是支持的:


ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count
filter
……


发表评论

暂无评论,抢个沙发...

客服 工单