当年今日入rss啦
众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
上星期已经把“当年今日”功能加到blog里(详细见Arm 2 W),从一开始就打算要加入rss的,但费尽九牛二虎之力都没做到,但今天,终于攻克了!
到底什么原因呢?
首先,我们要看我当年今日的源代码,利用
< ?php wp_today();? > |
完成插入,在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 32 33 34 | function wp_today(){ $title = "<h3>当年今日</h3>"; $limit = 10; $order = "latest"; $post = 1; $feed = 1; global $wpdb; $post_year = get_the_time('Y'); $post_month = get_the_time('m'); $post_day = get_the_time('j'); if($order == "latest"){ $order = "DESC";} else { $order = '';} $sql = "select ID, date(post_date_gmt) as h_date, post_title, comment_count FROM $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish' AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day' order by post_date_gmt $order limit $limit"; $histtory_post = $wpdb->get_results($sql); if( $histtory_post ){ foreach( $histtory_post as $post ){ $h_date = $post->h_date; $h_post_title = $post->post_title; $h_permalink = get_permalink( $post->ID ); $h_comments = $post->comment_count; $h_post .= "<li>$h_date -- <a href='".$h_permalink."' title='Permanent Link to ".$h_post_title."'>$h_post_title</a> <!--($h_comments) --></li>"; } } if ( $h_post ){ $result = "".$title."<ul>".$h_post."</ul>"; } echo $result; } |
这堆代码,要实现在blog的某个位置输出当年今日没有任何问题,但要注意,数据是用“echo $result;”完结的,这就埋下了伏笔。
我试过很多回,试图在function.php加入以下这堆代码调用上面的代码实现当年今日在rss中的输出:
1 2 3 4 5 6 7 8 9 10 | function wp_today_rss($content){ if (is_feed()){ $content = $content.wp_today(); return $content; } else { return $content; } } add_filter('the_content', 'wp_today_rss'); |
输出是能输出了,但结果很囧,见图:
当年今日输出在了正文前面!(那堆乱码东西是正文),但代码中我明明是这般写的“$content = $content.wp_today();”,东西应该加在后面的,但实际上却在前面,这让我百思不得其解。
无可奈何,今天试过用最笨的方法把wp_today()除echo $result;以外的数据全部往function wp_today_rss里放,能实现了我希望的效果,见图:
到底怎么办呢?难道要很笨地把同1段代码写2回?????
狂抓不已,于是就去请教别人了,但一请教,人家还没回答我就有了头绪,不如把上面两段代码拆分为3段,即:
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 | function get_today(){ $title = "<h3>当年今日</h3>"; $limit = 10; $order = "latest"; $post = 1; $feed = 1; global $wpdb; $post_year = get_the_time('Y'); $post_month = get_the_time('m'); $post_day = get_the_time('j'); if($order == "latest"){ $order = "DESC";} else { $order = '';} $sql = "select ID, date(post_date_gmt) as h_date, post_title, comment_count FROM $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish' AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day' order by post_date_gmt $order limit $limit"; $histtory_post = $wpdb->get_results($sql); if( $histtory_post ){ foreach( $histtory_post as $post ){ $h_date = $post->h_date; $h_post_title = $post->post_title; $h_permalink = get_permalink( $post->ID ); $h_comments = $post->comment_count; $h_post .= "<li>$h_date -- <a href='".$h_permalink."' title='Permanent Link to ".$h_post_title."'>$h_post_title</a> <!--($h_comments) --></li>"; } } if ( $h_post ){ $result = "".$title."<ul>".$h_post."</ul>"; } return $result; } function wp_today(){ echo get_today(); } function wp_today_rss($content){ if (is_feed()){ $content = $content.get_today(); return $content; } else { return $content; } } add_filter('the_content', 'wp_today_rss'); |
嘿嘿,对味!function get_today()是核心代码,function wp_today()用作来打印输出,function wp_today_rss()使之能加入到rss中。哈,“echo $result;”换成了“return $result;”解决了纠结我很久的千古难题。只能叹一句,从前的函数调用没领会透,也没有认真留意数据返回那些事。
最后,终于把问题解决了,爽得飞天,哈哈哈~~~~
PS:感谢SH童鞋的鼎力支持!
PS2:代码可能在rss中不能全部显示,有需要者请移步网页。
要是 正文 文字能像图片中那么大就好了。难道是我 显示器的问题 ?
看来真是你显示器的问题了。
很少看到技术型的女生啊。。这验证码,也太长了吧……= =+
嘻嘻,不知你撞到什么验证码呢?我一共设置了42个,最短的css、rss只有3个字母而已。