
对于主题开发者来说,WordPress 3.1 一个比较有看点的新功能是加入了文章样式(Post Formats)的支持。本文简单说一下在主题里如何添加和显示不同的文章样式。
首先打开主题的 functions.php 文件,添加下面的代码,增加 aside、chat、gallery、image、link、quote、status、video、audio 这几种文章样式。
// 添加文章样式
add_theme_support( 'post-formats', array( 'aside', 'chat', 'gallery',
做完这部后在写文章的时候会出现一个 Format 选项,根据自己的文章风格来选就行了,如果是微博体的话,就选 status 好了。

保存之后,在后台的文章页面也可以直观地看到文章样式。

但是不要忘记最重要的一步,我们需要在文章主循环里增加文章样式的判断,否则显示出来的文章都一个样式,文章格式就白设了。代码就像下面这样子,自己喜欢什么样式就改成什么样式就行了,本文做的只是简单的举例。
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<DIV id="post-<?php the_ID(); ?>" ? post_class(); <?php>>
<?php
if ( has_post_format( 'aside' )) {
echo the_content();
}
elseif ( has_post_format( 'chat' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
elseif ( has_post_format( 'gallery' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
elseif ( has_post_format( 'image' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_post_thumbnail('medium');
echo the_content();
}
elseif ( has_post_format( 'link' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
elseif ( has_post_format( 'quote' )) {
echo the_content();
}
elseif ( has_post_format( 'status' )) {
echo the_content();
}
elseif ( has_post_format( 'video' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
elseif ( has_post_format( 'audio' )) {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
else {
echo '
<h3>';
echo the_title();
echo '</h3>
';
echo the_content();
}
?>
</DIV>
<?php endwhile; else: endif; ?>
OK!大功告成。
原文地址:http://codecto.com/2010/11/wordpress-post-formats/