- 性别
- 男
- QQ
- 88271725
- 个人主页
- http://www.sahot.com
- 自我介绍
- 分享互联网热点资讯---- www.sahot.com
致力于互联网信息共享,关注和分享互联网动态。 - 原始股
- 0 股
- 在线时间
- 41 小时
- 帖子
- 941
- 记录
- 0
- 好友
- 0
- 主题
- 43
- 精华
- 0
- 最后登录
- 2011-10-4
- 注册时间
- 2008-8-2
- 阅读权限
- 70
- UID
- 57905
   
- 金钱
- 1158 WPB
- 积分
- 2073
|
发表于 2010-6-20 10:44:23
|显示全部楼层
23,设定文章从发布到出现在RSS中的时间长短
通过RSS订阅来阅读博文的朋友可能都会有这个体验:经常发现RSS中的文字或者细节有错误,而返回到页面的时候却发现错误已经没有了。这种情况最有可能是因为
RSS最大的好处是快捷、直接,但这个最大的好处有时候对作者来说却会引发某些尴尬。所以,有时候有必要让文章发布后到读者从RSS中按到有一个小小的时间差,方便作者排查某些问题。以下的代码可以做到以下几点:
-
- function publish_later_on_feed($where) {
- global $wpdb;
- if ( is_feed() ) {
- // timestamp in WP-format
- $now = gmdate(‘Y-m-d H:i:s’);
- // value for wait; + device
- $wait = ‘10′; // integer
- // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
- $device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
- // add SQL-sytax to default $where
- $where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
- }
- return $where;
- }
- add_filter(‘posts_where’, ‘publish_later_on_feed’);
复制代码
这段代码设置的时间是10分钟,你可以把10改成任何你想要的时间。
24,自定义摘要输出时的符号
一般设定自动摘要输出,你会经常在WordPress博客的首页看到“[。..]”这样的符号。为了界面的美观,或者是个性化的需要,你可以把这个默认的符号改变为其他的符号。而以下的代码就是为了实现这个而写:
-
- // custom excerpt ellipses for 2.9
- function custom_excerpt_more($more) {
- return '…';
- }
- add_filter('excerpt_more', 'custom_excerpt_more');
- /* custom excerpt ellipses for 2.8-
- function custom_excerpt_more($excerpt) {
- return str_replace('[...]', '…', $excerpt);
- }
- add_filter('wp_trim_excerpt', 'custom_excerpt_more');
- */
复制代码
25,自定义摘要输出的文字长度
假如你比较懒,不想在撰写文章的时候每篇文章都输入摘要,就可以让系统自动截取一定长度的文字来作为摘要输出。下面的代码默认是100个字节,也就是50个汉字。你可以把数值修改成符合你需要的数字。
-
- function new_excerpt_length($length) {
- return 100;
- }
- add_filter('excerpt_length', 'new_excerpt_length');
复制代码
26,显示精确评论数
WordPress默认是把trackbacks 和 pings 都算作评论的,因此当你设置不显示trackbacks 和 ping的时候,评论数看起来总是不对头。以下的代码则以让WordPress只计算评论的数量,而不把trackbacks 和 pings也计算进去。
-
- add_filter('get_comments_number', 'comment_count', 0);
- function comment_count( $count ) {
- if ( ! is_admin() ) {
- global $id;
- $comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
- return count($comments_by_type['comment']);
- } else {
- return $count;
- }
- }
复制代码
27,取消RSS输出
对于某些博客而言,或者因为被太多人采集了,或者因为不想让别人通过RSS订阅,想取消RSS输出。WordPress默认是没有这个功能的,但你可以通过以下的代码来取消RSS输出。
-
- function fb_disable_feed() {
- wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
- }
- add_action('do_feed', 'fb_disable_feed', 1);
- add_action('do_feed_rdf', 'fb_disable_feed', 1);
- add_action('do_feed_rss', 'fb_disable_feed', 1);
- add_action('do_feed_rss2', 'fb_disable_feed', 1);
- add_action('do_feed_atom', 'fb_disable_feed', 1);
复制代码
28,显示Twitter 的订阅数以及其他资料
Twitter系统以及很多第三方的客户端都可以让你在WordPress博客的侧边栏暂时Twitter的订阅数以及一些其他的资料。这种做法往往很多时候都没办法跟博客已有的界面结合的很好。而以下的代码则可以让你自定义Twitter 在博客上的显示外观。
-
- function rarst_twitter_user( $username, $field, $display = false ) {
- $interval = 3600;
- $cache = get_option('rarst_twitter_user');
- $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
- if ( false == $cache )
- $cache = array();
- // if first time request add placeholder and force update
- if ( !isset( $cache[$username][$field] ) ) {
- $cache[$username][$field] = NULL;
- $cache[$username]['lastcheck'] = 0;
- }
- // if outdated
- if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
- // holds decoded JSON data in memory
- static $memorycache;
- if ( isset($memorycache[$username]) ) {
- $data = $memorycache[$username];
- }
- else {
- $result = wp_remote_retrieve_body(wp_remote_request($url));
- $data = json_decode( $result );
- if ( is_object($data) )
- $memorycache[$username] = $data;
- }
- if ( is_object($data) ) {
- // update all fields, known to be requested
- foreach ($cache[$username] as $key => $value)
- if( isset($data->$key) )
- $cache[$username][$key] = $data->$key;
- $cache[$username]['lastcheck'] = time();
- }
- else {
- $cache[$username]['lastcheck'] = time()+60;
- }
- update_option( 'rarst_twitter_user', $cache );
- }
- if ( false != $display )
- echo $cache[$username][$field];
- return $cache[$username][$field];
- }
复制代码
把上面的代码复制到 functions.php后,再把下面代码复制到你想出现的地方即可。
-
- echo rarst_twitter_user('wpbeginner', 'name').' has '.
- rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
- rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
复制代码 |
|