官网: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?》