官網:https://developer.wordpress.org/reference/functions/get_post_type/
Retrieves the post type of the current post or of a given post.
獲得指定文章或當前文章的文章類型。
舉例:
<?php get_post_type( $post_id ) ?>
$post_id變數用來指定文章ID,如果該函數使用在主循環中則可省略。
一般在模板文件中,使用如下代碼:
get_template_part( 'template-parts/content', get_post_type() );
舉例2
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$post_type =get_post_type();
//假設id為36的文章類型為附件類型attachment
echo "post-type".$post_type;
//列印出文章類型
get_template_part( 'template-parts/content', get_post_type() );
}
}
?>
有哪些文章類型?
Post Types:https://developer.wordpress.org/themes/basics/post-types/
『aside』, 『gallery』, 『link』, 『image』, 『quote』, 『status』, 『video』, 『audio』, 『chat』
- Post (Post Type: 『post』)
- Page (Post Type: 『page』)
- Attachment (Post Type: 『attachment』)
- Revision (Post Type: 『revision』)
- Navigation menu (Post Type: 『nav_menu_item』)
get_post_type();
與POST格式一起使用。
功能在主題上得到擴展,包括:
add_theme_support( 『post-formats』, array( 『aside』, 『gallery』 ) );
將其包含在主題的functions.php文件中之後,選擇POST類型的選項(包含在數組中的)在創建/編輯帖子時,將出現在右側的側邊欄中。
如果沒有選擇POST類型,WordPress將選擇默認的Standard.
與POST格式一起使用。
功能在主題上得到擴展,包括:
add_theme_support( 『post-formats』, array( 『aside』, 『gallery』 ) );
將其包含在主題的functions.php文件中之後,選擇POST類型的選項(包含在數組中的)在創建/編輯帖子時,將出現在右側的側邊欄中。
如果沒有選擇POST類型,WordPress將選擇默認的Standard.
get_post_type和get_post_format區別?
get_post_format:獲得指定文章的自定義文章類型。
get_post_type:獲得的是諸如:「頁面」、「日誌」、「附件」這樣的文章本質類型
相關閱讀
《如何使用get_post_format?》