get_post_format()函數(shù)用于獲取文章的形式,在為不同形式的文章制作不同的模板時非常有用。
語法
get_post_format( int $post = null )
參數(shù)
$post_id 整數(shù)型,默認值:null 文章的ID
返回值
實例
get_template_part('content', get_post_format());
get_template_part():根據(jù)get_post_format()返回的信息來加載content開頭命名的相應的模板;
get_post_format():獲取當前post的形式。
講解
比如我們現(xiàn)在有一個模板 content-image.php
那么可以通過get_template_part(‘content’, get_post_format())調用,即如果當前post形式是image,也就是對應上面的返回值,那么就會調用模板content-image.php;如果返回的post分類模板不存在,那么就會使用默認post模板。