用静态化当然可以解决这些问题,不过对于流量不大的博客就没必要了。 wordpress自带有缓存体系,关键的函数你可以在wp-includes/cache.php或Codex里查找到所有的函数。
你可以使用下列函数来实现添加缓存。
复制代码 代码如下:wp_cache_add($key, $data, $flag = '', $expire = 0) 相反的,要删除缓存数据可以通过下面的方法实现。 复制代码 代码如下:wp_cache_delete($id, $flag = '') 复制代码 代码如下:wp_cache_get($id, $flag = '') 复制代码 代码如下:wp_cache_replace($key, $data, $flag = '', $expire = 0) 举个例子,如果想输出最新评论,可以先从缓存里读取需要输出的HTML,如果Target="_blank">没有,请求一次,成功后加入缓存,当然时间可以控制。
复制代码
代码如下:define('WP_CACHE', true);
如果你用了其它缓存插件,基本上这个就没效果了。因为这些插件有类似的方法。
复制代码
代码如下: $cacheID = md5($args);//有缓存就直接输出if($output = wp_cache_get('recentComments_'.$cacheID, 'Winysky')){echo $output;return;}
//$rcms = get_comments($args);global $wpdb;
$my_email = "'" . get_bloginfo ('admin_email') . "'";//自动获取博主邮箱$rcms = $wpdb->get_results("SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_contentFROM $wpdb->comments LEFT OUTER JOIN $wpdb->postsON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)WHERE comment_APProved = '1'AND comment_type = ''AND post_password = ''AND comment_author_email != $my_email
ORDER BY comment_Date_gmtDESC LIMIT 10
");
//print_r($rcms);return;if(empty($rcms)){_e('没有数据');return;}//历遍数据$output = '';foreach( $rcms as $rcm ){$author = $rcm->comment_author;//if($author =='admin'){continue;}$content = DeamworkStriptags( $rcm->comment_content);$the_title = get_the_title($rcm->comment_post_ID);$s_excerpt = convert_smilies( DeamworkSubstr( $content, 200 ) );$contents = ' on ' . $the_title . '
$output .= '
}//输出后加入缓存wp_cache_add('recentComments_'.$cacheID,$output, 'Deamwork');echo $output;}
最后,试试效果吧~














发表评论