Post Format(文章格式)是wordpress 3.1引入的一个新的特征。主题可以根据post format的不同以不同的方式来展示文章。wordpress提供了一些标准的post format,作为wordpress主题的开发者不必去支持每一种post format,当然也可不支持任何一种。post format的类型不能通过主题或者插件进行添加。
简单的说,如果一个wordpress主题支持post format,当文章发布时可以选择post format类型来以不同的方式来展示这个篇文章。
以前可能通过文章的不同分类、或者根据post_class()来定制文章的展示形式。使用post format可以更加专注地使用CSS来进行美化文章展示。
这个功能让主题支持 Post Formats 功能,这个功能是 3.1 版本引进的,当使用子主题(Child Themes)的时候,注意 add_theme_support( ‘post-formats’ ) 会覆盖父主题(Parent Themes)定义的 Formats,而不是额外增加。
让主题支持特定的 Post Formats,使用:
add_theme_support(‘post-formats’,array(‘aside’,’gallery’));
检查一个主题是否给 post 指定了 ‘quote’ 这类 post format:
if( has_post_format(‘quote’)){echo’This is a quote.’;}
————————————————
添加主题支持
如果想让主题支持post format,只需将add_theme_support()函数添加在functions.php
中即可,如(来自2021主题):
/**
* Add post-formats support.
*/
add_theme_support(
'post-formats',
array(
'link',
'aside',
'gallery',
'image',
'quote',
'status',
'video',
'audio',
'chat',
)
);
添加“页面”或“自定义post”支持
上面代码默认只是添加了post类文章可选post format。如果想让页面(Page)或者自定义post类型支持post format,需要使用add_post_type_support()函数,同样将其放在functions.php
中。如:
// add post-formats to post_type 'page'
add_post_type_support( 'page', 'post-formats' );
// add post-formats to post_type 'my_custom_post_type'
add_post_type_support( 'my_custom_post_type', 'post-formats' );
添加文章,选择文章格式
文章格式默认为standard类型
主题中如何使用post format
在主题中使用get_post_format()来检测文章类型。如果文章类型为standard类型,则返回false
。也可以使用has_post_format()来测试。
if( has_post_format( 'video')) {
echo 'this is the video format';
}
通过在文章的wrapper结构中调用post_class()函数,将添加额外的样式类,然后通过样式表进行布局或美化。如:
.format-video .post-title {
display: none;
}
参考: