21,不通过.htaccess将rss地址唯一化
WordPress本身提供好几个不同版本的rss地址,加入你又使用了FeedBurner或者feedsky的话,RSS地址就会更多。太多的 RSS容易分流订阅客户,而且也不利于品牌推广。
一般的修改方法是通过更改.htaccess来进行,此外,还可以通过以下的代码来实现。
function custom_feed_link($output, $feed) {
$feed_url = 'http://feeds.feedburner.com/wpbeginner';
$feed_array = array('rss' => $feed_url, 'rss2' => $feed_url, 'atom' => $feed_url, 'rdf' => $feed_url, 'comments_rss2' => '');
$feed_array[$feed] = $feed_url;
$output = $feed_array[$feed];
return $output;
}
function other_feed_links($link) {
$link = 'http://feeds.feedburner.com/wpbeginner';
return $link;
}
//Add our functions to the specific filters
add_filter('feed_link','custom_feed_link', 1, 2);
add_filter('category_feed_link', 'other_feed_links');
add_filter('author_feed_link', 'other_feed_links');
add_filter('tag_feed_link','other_feed_links');
add_filter('search_feed_link','other_feed_links');
22,启用paypal 捐赠简码
当你写完一篇以后,可以在文章里面插入paypal 捐赠按钮,方便读者捐赠。以下的代码可以让你非常轻松的做到这一点。
function donate_shortcode( $atts ) {
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 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中按到有一个小小的时间差,方便作者排查某些问题。以下的代码可以做到以下几点:
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.';
转自站长站.











[手册]28个实用的WordPress主题函数使用技巧 有 9 篇评论
初次拜访,大力支持
如果你的博客接受读者的投稿,想在该篇文章出现投稿者的姓名,同时又不想通过添加作者的这种繁琐而麻烦的方式来操作,则可以使用下面的代码。使用下面的代码后,只需要在撰写文章的时候在自定义区域填上投稿者的姓名即可。系统会自动将发布者的名称换成投稿者的名称。
这个代码对接受读者投稿较多的网站,或者是资讯型的网站非常有用(利用它来显示来源)。
好主意,谢谢焰火mm。
评论样式真不错... 👿
博主,立正,我来报道。
世界杯,加油,呵呵
嗯哼,世界杯加油~
学习了....^^
太喜欢这个评论样啊啊了 😆