28个非常有用的WordPress 主题函数使用技巧(4)
接前3篇:
注:本文提到的代码均必须加到 functions.php 文件里面。注意代码里面涉及到网址,邮件等内容可以自行替换。
22,启用paypal 捐赠简码
当你写完一篇以后,可以在文章里面插入paypal 捐赠按钮,方便读者捐赠。以下的代码可以让你非常轻松的做到这一点。
extract(shortcode_atts(array(
'text' => 'Make a donation',
'account' => 'REPLACE ME',
'for' => '',
), $atts));
global $post;
if (!$for) $for = str_replace(" "," ",$post->post_title);
return '<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation for '.$for.'">'.$text.'</a>';
}
add_shortcode('donate', 'donate_shortcode');
23,设定文章从发布到出现在RSS中的时间长短
通过RSS订阅来阅读博文的朋友可能都会有这个体验:经常发现RSS中的文字或者细节有错误,而返回到页面的时候却发现错误已经没有了。这种情况最有可能是因为
RSS最大的好处是快捷、直接,但这个最大的好处有时候对作者来说却会引发某些尴尬。所以,有时候有必要让文章发布后到读者从RSS中按到有一个小小的时间差,方便作者排查某些问题。以下的代码可以做到以下几点:
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博客的首页看到“[...]”这样的符号。为了界面的美观,或者是个性化的需要,你可以把这个默认的符号改变为其他的符号。而以下的代码就是为了实现这个而写:
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个汉字。你可以把数值修改成符合你需要的数字。
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
26,显示精确评论数
WordPress默认是把trackbacks 和 pings 都算作评论的,因此当你设置不显示trackbacks 和 ping的时候,评论数看起来总是不对头。以下的代码则以让WordPress只计算评论的数量,而不把trackbacks 和 pings也计算进去。
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输出。
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 在博客上的显示外观。
$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后,再把下面代码复制到你想出现的地方即可。
Then place the following code where you want to display the count in your theme file:
rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
写付费评论赚美金
- Postlinks:自动撰写付费评论和付费文章链接,无最低起付标准,无手续费,英文站必做 >>详细了解
- SponsoredReviews:5-500刀/篇,数量多,50%手续费 >>详细了解
- LinkWorth:5-10刀/篇,数量一般,30%手续费 >>详细了解
- ReviewMe:5-20刀/篇,无最低起付标准,无手续费 >>详细了解
国外广告联盟推荐
- Clicksor:50美金起付,paypal收款 >>详细了解
- BidVertiser:提供CPC和CPM广告,10美金起付 >>详细了解
- PaidSurveysLive:调查赚钱,0.01美金起付 >>详细了解
下载赚钱
- 最高1000次下载赚150元,做1M以内中文流量下载站(电子书、杂志、软件等)的首选。20远起付,支持支付宝。每周结算一次,信誉高 >>详细了解
- 手机电子书下载|UMD小说下载|TXT小说下载|PDF小说下载|花花公子下载|经济学书籍下载|CHM电子书下载
- ELL|17岁|女性健康|男士健康|瑜伽杂志|家庭影院|金属音锤|名车志|读者文摘|Mac Life|Make杂志|木工艺术

老曾,关注你这个专题很久了,我觉得这些技巧国内的都用不太到哎…
呵呵,俺留着自己 用的:)
沙发拿下、。这个系列好多内容啊
留个记号 下次来看!
WordPress就是好,有这么多主题可用。
曾哥,你的虚拟主机哪的啊,能否推荐下,qq530468003.
梦游科技。www.meyu.net
多谢,有几项很实用.
太难了,没看明白
很经典的系列,不知道还有不有续集啊。现在在研究MU,很多这里面的代码都能用上,还有一些代码给了我很大的提示。感谢作者的无私分享。呵呵
我是小曾,这些内容在我这个新手看来好费解呦!!!
能不能把博主给别人回复的评论从评论数中剔除呢