
上个月就想搞这个东西的,但是一直以来都没有方法,也不知道怎么弄。具体的效果呢,像ipc.me等很多网站在GR里面观看的时候,只看到部分内容,需要点击链接到网站才可以看全文的。我很喜欢这种方式,可是不知道怎么弄。上个月问过露兜,露兜给了我方法,才得以开始这个工作。昨天终于弄成功了。哇咔咔。那么我现在写个教程贴,省的以后忘记了。
效果预览(在Google Reader中的效果):

具体做法:
1、文章截断。
就是截取一部分文章内容,多余的用查看全文来显示。
我这里采用了两种方法,普通的方式是根据字数进行截断,使用函数mb_strimwidth($content , 0, 500, '.....')。
而我的比较特别,由于我写文章习惯自己加more标签,这样的话,首页只显示部分,more标签在数据库的具体表示<!--more-->,但是在文章中输出的时候是显示<span id=''more-文章ID"></span>,所以我通过mb_strstr($content,</span>,ture)进行截断。
但是还是有点问题的,如果more标签前面有个span标签的话,那就毁了,所以这个要养成个人习惯。另外,如果整篇文章没有span标签的话,那不就整篇输出了?所以我这边做了一个判断,如果内容包括span标签,那么就通过截取span前面的显示,如果内容不包括span标签的话,就通过一定字数进行截断,这里要注意的是图片也算字数的,具体字数的话不清楚了,可以自己在本地测试一下。
2、相关文章。
在RSS输出下下部显示几篇相关文章,我这边一共参考了三种方式。
露兜的《WordPress代码实现相关文章的几种方法》中第三种标签相关,SQL获取,我自己改进了下。首先如果没有相关标签的话,它会显示空白,应该加一个else的。另外,如果通过标签查找文章数目不够的话,从分类中获取文章补全个数,如果还是不足的话,我也没办法了,其实还是可以不全的,但是我没弄。
zww的《willin相关文章代码修正》中的通过文章标签获取相关文章,其中的the_title()函数放在我这边不知为何一直获取不了,改为get_the_title()才可以。他的理念是,先通过标签取相关文章,如果不够用分类中的文章补齐。
还有一种是随机生成相关文章的方式,我忘记出处了,汗 -_-!,但是放在我这边不成功,请自行测试一下。
其实,还有一种方式,就是用无觅相关文章的方式,但是我没事实验,有兴趣的可以自己试试看,爱软就是用的这种方式。
3、其他链接。
增加copyright和新浪微博链接等等,可根据自己喜好增加。
4、分享链接。
想用jiathis的代码实现分享的,但是用工具式、图标式都不成功,貌似RSS里面不能加载js的,只好用了按钮式,它是直接显示的图片和链接。
具体代码:
请将此代码加在主题的functions.php里面(加之前最好坐下备份),注意php标签的关闭。另外,在WordPress选项里面阅读设置,对于 feed 中的每篇文章,显示 全文。否则,图片啥的都没有了。
代码里面的一些链接是我自己的,请根据需要自行修改。谢谢。
方法一(可靠):
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| function feed_copyright($content) {
// http://www.ludou.org/how-to-generate-related-posts-in-wordpress.html
global $post;
global $wpdb;
$related = ';
// Laycher : Edit
//如果有 more标签即span标签,那么就截断more以前的。如果没有,则通过字数截断
if(strpos($content,'') ){
$content = mb_strstr($content,'',ture).'. get_permalink().'" target="_blank" rel="nofollow">更多Read More... ';
//
} else {
$content = mb_strimwidth($content , 0, 500, '.....').'. get_permalink().'" target="_blank" rel="nofollow">更多Read More... ';
}
$post_tags = wp_get_post_tags($post->ID);
// 通过修改数字 5,可修改你想要的文章数量
$i = 5;
if ($post_tags) {
$tag_list = '';
$tag_out = '';
foreach ($post_tags as $tag)
{
// 获取标签列表
$tag_list .= $tag->term_id.',';
}
$tag_list = substr($tag_list, 0, strlen($tag_list)-1);
$related_posts = $wpdb->get_results("SELECT post_title, ID FROM posts, term_relationships, term_taxonomy WHERE term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id AND ID = object_id AND taxonomy = 'post_tag' AND post_status = 'publish' AND post_type = 'post' AND term_id IN (" . $tag_list . ") AND ID != '" . $post->ID . "' ORDER BY RAND() LIMIT'".$i."'");
if ( $related_posts ) {
foreach ($related_posts as $related_post) {
$related = $related.'.get_permalink($related_post->ID).'" rel="bookmark" title="'.$related_post->post_title.'">'.$related_post->post_title.' ';
}
} else {
// Laycher:edit 如果没有这个标签的其他文章用同类别的文章代替,如果同类别的也没有,那么有多少显示多少,这里还要再优化的,有兴趣的可以继续写。
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i);
query_posts($args);
while( have_posts() ) {
the_post();
$related=$related.'.get_permalink().'" title="'.get_the_title(). '">'.get_the_title().'';
} wp_reset_query();
//$related = $related.'暂无相关文章';
}
} else {
$related = $related.'暂无相关文章';
}
$related = ' Laycher 猜你还喜欢这些文章:'.$related.'';
$about = ' Copyright ©2010-2012 ¦ RSS订阅 ¦ 新浪微博 ¦.get_permalink().'" title="'.get_the_title().'" target="_blank">本文链接 ¦ .get_permalink().'#comments" title="'.get_the_title().'的评论" target="_blank">添加评论 ';
$share = '';
return $content.$related.$about.$share;
}
add_filter ('the_excerpt_rss', 'feed_copyright');
add_filter('the_content_feed', 'feed_copyright'); |
方法二(可靠):
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| //http://zww.me/archives/25353
function feed_copyright($content) {
// Laycher : Edit
//如果有 more标签即span标签,那么就截断more以前的。如果没有,则通过字数截断
if(strpos($content,'') ){
$content = mb_strstr($content,'',ture).'. get_permalink().'" target="_blank" rel="nofollow">[ 查看全文... Read More... ] ';
} else {
$content = mb_strimwidth($content , 0, 500, '.....').'. get_permalink().'" target="_blank" rel="nofollow">[ 查看全文... Read More... ] ';
}
$post_num = 5; // 數量設定.
$exclude_id = $post->ID; // 單獨使用要開此行 //zww: edit
$posttags = get_the_tags();
$i = 0;
$related = ';
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags), // 只選 tags 的文章. //zww: edit
'post__not_in' => explode(',', $exclude_id), // 排除已出現過的文章.
'caller_get_posts' => 1,
'orderby' => 'comment_date', // 依評論日期排序.
'posts_per_page' => $post_num
);
query_posts($args);
while( have_posts() ) {
the_post();
$related=$related.'.get_permalink().'" title="'.get_the_title(). '">'.get_the_title().''; //laycher:edit
$exclude_id .= ',' . $post->ID;
$i++;
} wp_reset_query();
}
if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats), // 只選 category 的文章.
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) {
the_post();
$related=$related.'.get_permalink().'" title="'.get_the_title(). '">'.get_the_title().'';//Laycher:edit
$i++;
} wp_reset_query();
}
if ( $i == 0 ) $related = '没有相关文章!';
// Laycher : edit
$related = 'Laycher 猜你还喜欢这些文章:'.$related.'';
$about = ' Copyright ©2010-2012 ¦ RSS订阅 ¦ 新浪微博 ¦.get_permalink().'" title="'.get_the_title().'" target="_blank">本文链接 ¦ .get_permalink().'#comments" title="'.get_the_title().'的评论" target="_blank">添加评论 ';
$share = '';
return $content.$related.$about.$share;
}
add_filter ('the_excerpt_rss', 'feed_copyright');
add_filter('the_content_feed', 'feed_copyright'); |
方法三(请测试后使用):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| // 随机方式,我这里不成功,希望你那可以成功
function feed_copyright($content) {
$content = mb_strimwidth($content , 0, 300, '.....').'. get_permalink().'" target="_blank" rel="nofollow">更多Read More... ';
$related = ';
$rand_posts = get_posts('numberposts=5&orderby=rand');
foreach( $rand_posts as $post ):
$related = $related.'.get_permalink().'">'. get_the_title().'';
endforeach;
$related = 'Laycher 猜你还喜欢这些文章:'.$related.'';
$about = ' Copyright ©2010-2012 ¦ RSS订阅 ¦ 新浪微博 ¦.get_permalink().'" title="'.get_the_title().'" target="_blank">本文链接 ¦ .get_permalink().'#comments" title="'.get_the_title().'的评论" target="_blank">添加评论 ';
$share = '';
return $content.$related.$about.$share;
}
add_filter ('the_excerpt_rss', 'feed_copyright');
add_filter('the_content_feed', 'feed_copyright'); |
PS:如果RSS输出是乱码的话,我估计你的编码方式有问题,把functions.php文件用UTF-8的方式保存。如果还不行的话,在header.php中加上
header("Content-Type:text/html;charset=gb2312");
如果还有什么疑问的话,可以共同讨论进步,谢谢。~。~
GoodGood
整了好长时间了……
终于找到了,谢谢啊。
我试了,真的不错,但就是有一个问题,相关文章中,把当前的文章也列上了。
还有,我本身使用了WordPress Related Posts插件我想用手动的方式(直接使用wp_related_posts())来显示相关日志,但总是显示到文章的前面,头疼啊。。。。。。。。
有方法的,请网上找下代码。
这个一定要试一下了!
问个问题,function custom_rss_feed_content($content) {
if(is_feed()) { //只在Feed中执行
$content = $content . “123456”;
}
return $content;
}
add_filter(‘the_content’,’custom_rss_feed_content’); 这样最简单的办法,在rss页面无法出现。是什么原因?
已经解决,估计是浏览器缓存问题,一直没清除。打扰了
28000K Final Fantasy XIV Gil in Ridill [JP Server] Sold, Customer Left the Review: I have bought ffxiv gil for many times,always the delivery within 15 minutes.