WordPress 3.1 如何添加文章样式

王朝学院·作者佚名  2016-04-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

对于主题开发者来说,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/

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航