get_post_format()
get_post_format( int|object|null $post = null )
检索帖子的格式段塞
Retrieve the format slug for a post
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$post | (int | object | null) | 可选 | Post ID或Post对象。可选,默认值是来自循环的当前post。 |
返回(Return)
(string|false)成功时的格式。否则就错了。
源码(Source)
/** * Retrieve the format slug for a post * * @since 3.1.0 * * @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop. * @return string|false The format if successful. False otherwise. */ function get_post_format( $post = null ) { if ( ! $post = get_post( $post ) ) return false; if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) return false; $_format = get_the_terms( $post->ID, 'post_format' ); if ( empty( $_format ) ) return false; $format = reset( $_format ); return str_replace('post-format-', '', $format->slug ); }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
3.1.0 | wp-includes/post-formats.php:17 | 10 | 3 |
笔记(Notes)
模板中的使用
例子
get_post_format() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_post_format/