文章形式是wordpress内置的功能,提供包括日志、图像、视频等多种文章格式,博主发布文章的时候可以根据文章的类型,在编辑页面的右侧选择对应的文章形式。这个功能让博主划分不同类型文章的同时便于博主给不同形式的文章单独设计显示形式,而且不受分类限制。

给主题添加文章形式功能:
要使用wordpress文章形式,需要让当前使用的主题支持文章形式,在主题的functions.php文件,添加以下代码:
add_theme_support( 'post-formats', array( 'aside', 'chat','gallery','image','link', 'quote', 'status', 'video', 'audio' ) );
参数说明:
- 日志(aside) – 典型样式就是没有标题。类似于 Facebook 或人人网中更新的一条日志。
- 相册(gallery) – 图像陈列厅。文章中通常会有“gallery”代码和相应的图像附件。
- 链接(link) – 链接到其它网站的链接。主题可能会使用文章中的第一个 <a href=””> 标签作为文章的外部链接。有可能有的文章至包含一个 URL,那么这个 URL 将会被使用;同时,文章标题(post_title)将会是附加到它的锚的名称。
- 图像(image) – 单张图像。文章中的首个 <img /> 标记将会被认为是该图片。另外,如果文章只包含一个 URL 链接,则被认为是该图片的 URL 地址,而文章标题(post_title)将会作为图片的标题属性。
- 引语(quote) – 引用他人的一段话。通常使用 blockquote 来包裹引用内容。或者,可能直接将引语写入文章,并将其出处写在标题栏。
- 状态(status) – 简短更新,通常最多 140 个字符。类似于微博 Twitter 状态消息。
- 视频(video) – 单一视频。文章中第一个 <video /> 或 object 或 embed 将被作为视频处理。或者,文章可以仅包含视频的 URL,甚至一些主题和插件可以支持自动嵌入您的文章附件中的视频。
- 音频(audio) – 一个音频文件。可以用于播客(podcasting)等。
- 聊天(chat) – 聊天记录
不同形式文章调用不同文章模板
默认情况下,wordpress文章统一调用single.php模板,那么怎么给不同的文章格式调用不同的文章模板?
方法一:
1、把当前主题的single.php文件命名为content.php,然后重新创建一个single.php文件,添加以下代码:
<?php get_template_part( 'content', get_post_format() ); ?>
2、把各个文章形式要调用的模板命名为content-{post-format}.php格式,如
- Standard:content.php
- Aside:content-aside.php
- Link:content-link.php
- Image:content-image.php
- Quote:content-quote.php
- Status:content-status.php
- Video:content-video.php
- Audio:content-audio.php
- Chat:content-chat.php
方法二:
1、在主题的functions.php文件添加以下代码:
add_action('template_include', 'load_single_template');
function load_single_template($template) {
$new_template = '';
if( is_single() ) {
global $post;
if ( has_post_format( 'aside' )) {
$new_template = locate_template(array('single-aside.php' ));
}elseif(has_post_format( 'link' )){
$new_template = locate_template(array('single-link.php' ));
} elseif(has_post_format( 'image' )){
$new_template = locate_template(array('single-image.php' ));
} elseif(has_post_format( 'quote' )){
$new_template = locate_template(array('single-quote.php' ));
} elseif(has_post_format( 'status' )){
$new_template = locate_template(array('single-status.php' ));
} elseif(has_post_format( 'video' )){
$new_template = locate_template(array('single-video.php' ));
} elseif(has_post_format( 'audio' )){
$new_template = locate_template(array('single-audio.php' ));
} elseif(has_post_format( 'chat' )){
$new_template = locate_template(array('single-chat.php' ));
} else{
$new_template = locate_template(array('single.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}
2、创建以下php文件:
- Standard:single.php
- Aside:single-aside.php
- Link:single-link.php
- Image:single-image.php
- Quote:single-quote.php
- Status:single-status.php
- Video:single-video.php
- Audio:single-audio.php
- Chat:single-chat.php
文章形式判断代码:
<?php if( has_post_format( 'status' )) { //状态 ?>
状态样式
<?php } else if ( has_post_format( 'aside' )) { //日志 ?>
日志样式
<?php } else if ( has_post_format( 'gallery' )) { //相册 ?>
相册样式
<?php } else if ( has_post_format( 'video' )) { //视频 ?>
视频样式
<?php } else if ( has_post_format( 'audio' )) { //音乐 ?>
音乐样式
//....
<?php } else{ //标准 ?>
常规样式
<?php } ?>
扩展:让页面和自定义文章类型支持文章形式
在主题的functions.php文件添加以下代码,把my_custom_post_type
改为自定义文章类型名称:
add_post_type_support( 'page', 'post-formats' );
add_post_type_support( 'my_custom_post_type', 'post-formats' );
最后可以根据自己的需要设计不同的模板!