不亦乐乎 » Wordpress » 不用more标签首页内容摘要显示

不用more标签首页内容摘要显示

2010年01月24日 2,294人路过 参与围观19人围观

如果想在Wordpress首页显示日志摘要,就得在发表文章的时候,在某一位置输入more标签,个人很不习惯这个方式,觉得很麻烦,所以一直以来都是用另外一个自动截取日志部分内容的方式来显示,这样就可以不用每次写日志要插入more标签了。修改方法如下:

在主题文件夹下的index.php文件里,找到如下代码(以默认的主题为例子):

<?php the_content(__('Read the rest of this entry &raquo;', 'kubrick')); ?>

替换成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 
	$permalink=get_permalink($ID);
	if(empty($post->post_password)){
	if (!(strtoupper(get_locale()) == 'ZH_CN')){
	echo '<p>';
	the_content_rss('', true, '', 55);
	echo '</p>';
	}else{
	$exp=mb_substr(strip_tags($post->post_content),0,300);
	echo '<p>'.$exp.' ......  ';
	}
	_e("</p><div class=\"morelink\"><a href=\"$permalink\" class=\"more\">阅读全文 ...</a></div>");
	}else{
	_e("<p>Protected post, Enter your password to view: ");
	_e("<a href=\"$permalink\">Click me to go and  enter the password &raquo;</a></p>");
	}
?>

可以通过修改上面代码$exp=mb_substr(strip_tags($post->post_content),0,300);后面的300数值的大小来控制首页需要输出的内容大小。另外,还可以通过css独立控制“阅读全文”的样式。

上面这个方法的缺陷在于,首页输出的摘要内容不包括里面链接和图片,对于除了文字外的内容会自动过滤(到首页看看我这篇日志的摘要,你就明白了。),只是纯文字内容。对于追求图文并茂的童鞋来说,相当的遗憾。另外,每次换主题,如果没有替换上面代码,则首页都是全文内容的输出。

不过图片的输出可以通过另外独立的php代码来实现,或者通过wordpress的自定义域来实现,只是自定义域的使用也是比较麻烦的就是了。

同时还有使用插件实现more标签功能的方法,如果你不介意在自己原有插件基础上再多加插件的话:

1、wp-kit-cn

wp-kit-cn是个不错的插件,最新评论输出,最新文章输出,N天内留言最多用户输出,本周或本月内留言最多用户输出,随机文章输出,评论最多的文章输出,最近回响输出,自动摘要算法,更加适合中文使用,wp-kit-cn官方下载地址

使用时,要把the_content函数改为the_excerpt函数才能截断…

不过wp-kit-cn实现的效果和我上面提供的代码一样,不能显示图片。

2、wp-limit-posts-automatically

wp-limit-posts-automatically官方下载地址,这个插件支持摘要输出图片和视频等,不过遗憾的是据说对中文支持不大友好。不过你可以下载这个插件经过牛人修改后的版本wp-summary-automatically

修改后的插件可以设置前几个段落为摘要,默认是前3段,不用修改the_content为the_excerpt了,如果没有设置不足设置的段落数,就不显示”阅读更多”链接词了。

3、通过function.php文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function my_excerpts($content = false) {
        // If is the home page, an archive, or search results
        if(is_front_page() || is_archive() || is_search()) :
                global $post;
                $content = $post->post_excerpt;
 
        // If an excerpt is set in the Optional Excerpt box
                if($content) :
                        $content = apply_filters('the_excerpt', $content);
 
        // If no excerpt is set
                else :
                        $content = $post->post_content;
                        $excerpt_length = 55;
                        $words = explode(' ', $content, $excerpt_length + 1);
                        if(count($words) > $excerpt_length) :
                                array_pop($words);
                                array_push($words, '...');
                                $content = implode(' ', $words);
                        endif;
                        $content = '<p>' . $content . '</p>';
 
                endif;
        endif;
 
// Make sure to return the content
        return $content;
 
}
 
add_filter('the_content', 'my_excerpts');

无觅相关文章插件,快速提升流量

分类:Wordpress, 标签:, ,
你可以通过 RSS 2.0 订阅关注本日志评论, 也可以直接 参与围观, 或 trackback 到你的博客上.
本日志地址: http://www.happyet.org/364.html, 转载请注明出处!

“不用more标签首页内容摘要显示”上有 19 条评论

  1. 万戈 说道:

    我用的就是这个方法,不过这个函数必须要PHP5才支持,不过WP2.9必须是PHP5了,呵呵

    • LMS 说道:

      你的主题有设置按分类显示图片,弥补了这种方法不显示图片的不足,不过按分类显示图片也就是你的主题适合使用,刚好我不怎么感冒图片,不然还得去找个自动显示图片简单点的方法。

    • 冰古 说道:

      mb函数不一定要php5,只是这个函数php默认安装是不包含的。

  2. that5 说道:

    不知道咋个的,我更喜欢用more的办法 :grin: ,自定义域很强大,大有前途。

  3. jason 说道:

    俺是懒人,直接用插件。。。

  4. jason 说道:

    博主,你这个博客静态化了吗?

  5. sosohehe 说道:

    以前是用插件 现在还是用more了 感觉这样更灵活一些

  6. 左岸读书 说道:

    直接用摘要函数不是很好吗?

  7. 在长春 说道:

    这个方法对中文支持好吗?会出现乱码否?

留下足迹