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