WordPress 默认主题在首页的文章显示都是全文,导致首页又臭又长,虽然可以手动用More标签来进行切割,但是每次都手动还是比较麻烦,下面提供几种方法来实现首页展示文章的自动摘要:
1、修改 content.php文件,在twentytwelve主题里,找到
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
修改为:
<?php if(!is_single()) { the_excerpt();} else {the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) );} ?>
好了,刷新首页,是不是发现首页自动实现摘要了,关于字数上的修改,可以在function.php文件里,加入以下代码:
function custom_excerpt_length( $length ) { return 200; //填写需要截取的字符数,1个汉字=2个字符 } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
如果需要修改结尾的显示方式,将省略号修改成该文章的链接,用以下方法:
function new_excerpt_more($more) { global $post; return ' <a href="'. get_permalink($post->ID) . '">Read More</a>'; } add_filter('excerpt_more', 'new_excerpt_more');
2、插件实现
当然,你也可以利用插件实现以上效果:
wp-utf8-excerpt http://wordpress.org/plugins/wp-utf8-excerpt/