f
wordpress七牛云存储cdn优化加速 代码插件双方法
wordpress七牛云存储cdn优化加速 代码插件双方法
二呆 7年前 (2017-09-07) wordpress优化  #wordpress# 
浏览:6237

引言:此文由子域名转移而来,因为细微强迫症和放弃子域名而不舍得完全丢弃,所以将会逐步第二次转移文章到主域名上来,二者主题(阿里白秀和D8)均来自大前端,追求完美的同时有一丝小懒,主题就不换了,D8主题用起来挺好。

/tmp/phpMvfmMC

相信很多站长都有用免费图床的习惯,它可以节省自己空间大小,但小小的图床有时也很苦恼呢~免费图床始终是别人的空间,不是自己的,肯定会有风险问题,有时有的站长会做两手准备,在本地存一份。还有,就算确定了用别人的图床,但还有国内和国外之分,国外的空间大但速度慢,国内的空间小但速度快,是不是很头疼?接下来就开始介绍七牛云存储,他是利用cdn加速来优化网站的css、js、图片等html代码的。关键是七牛云存储拥有大流量大空间,让你用到爽歪歪~

七牛缩略图功能:

1、日志缩略图

  1. <?php  if(wpjam_has_post_thumbnail()){?>
  2. <div class="entry-thumb">
  3.     <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php wpjam_post_thumbnail(array(150,150),$crop=1);?></a>
  4. </div>
  5. <?php } ?>

这个函数有两个参数:

$size:设置缩略图的大小,它是一个数组,比如上面例子中就是设置缩略图大小为:宽是 150px,高也是 150px。
$crop:设置是否裁剪缩略图,1为裁剪,如果为0,则只是按照最大边进行缩放,不进行裁剪。
另外这个函数相比 WordPress 默认的 the_post_thumbnail 函数相比还有一个强大的地方是,如果没有设置缩略图,它将自动获取第一张图片作为缩略图。

至于上面代码放到什么地方,我只能说你主题原来the_post_thumbnail()函数是在放到哪里,这个函数就放到哪里。

2、日志列表缩略图

⑴相关日志:

  1. wpjam_related_posts($number=5, $args);

在需要显示相关日志的地方插入以上模板函数,其中:

$number:相关日志显示数量。
$arg:相关日志显示的参数,其参数默认为。

  1. array(
  2. 'class'=>,                    //外层ul的class。
  3. 'thumb' => true,                //是否带缩略图,默认带
  4. 'size' => 'thumbnail',          //缩略图大小
  5. 'crop'=> true,                  //缩略图是否裁剪
  6. 'thumb_class'=>'wp-post-image', //缩略图的class
  7. 'number_per_row'=>5             //如果设置为缩略图为横排,每行个数
  8. );

⑵最新日志:

  1. wpjam_new_posts($number=5, $post_type="post"$argsarray());

$number:最新日志显示数量。
$post_type:最新日志类型。
$arg:最新日志显示的参数,默认和相关日志$arg参数一样。

⑶最热日志:

  1. wpjam_top_viewd_posts($number=5, $days=0, $argsarray());

$number:最热日志显示数量。
$days:从最新多少天内获取最热日志,默认0为所有。
$arg:最新日志显示的参数,默认和相关日志$arg参数一样。
下载地址:

百度网盘下载
千脑网盘下载

因为七牛云存储还是会存在把图片放到别人空间的分享,加上wordpress安装各种插件会影响网站的效率,所以以下分享非插件方法,来优化你的wordpress网站,将以下代码放到function.php中并更改为自己的域名即可。

  1. define('FocusCDNHost','http://www.tongleer.com');//wordpress网站网址
  2. define('FocusCDNRemote','http://qiniudn.tongleer.com');//cdn域名
  3. define('FocusCDNIncludes','wp-content,wp-includes');//设置加速目录
  4. define('FocusCDNExcludes','.php|.xml|.html|.po|.mo');//设置文件白名单
  5. define('FocusCDNRelative',);//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix.
  6. function do_cdnrewrite_ob_start() {
  7.     $rewriter = new FocusCDNRewriteWordpress();
  8.     $rewriter->register_as_output_buffer();
  9. }
  10. add_action('template_redirect', 'do_cdnrewrite_ob_start');
  11. class FocusCDNRewriteWordpress extends FocusCDNRewrite{
  12.     function __construct() {
  13.         $excl_tmp = FocusCDNExcludes;
  14.         $excludes = array_map('trim', explode('|', $excl_tmp));
  15.         parent::__construct(
  16.             FocusCDNHost,
  17.             FocusCDNRemote,
  18.             FocusCDNIncludes,
  19.             $excludes,
  20.             !!FocusCDNRelative
  21.         );
  22.     }
  23.     public function register_as_output_buffer() {
  24.         if ($this->blog_url != FocusCDNRemote) {
  25.             ob_start(array(&$this, 'rewrite'));
  26.         }
  27.     }
  28. }
  29. class FocusCDNRewrite {
  30.     var $blog_url    = null;
  31.     var $cdn_url     = null;
  32.     var $include_dirs   = null;
  33.     var $excludes    = array();
  34.     var $rootrelative   = false;
  35.     function __construct($blog_url$cdn_url$include_dirsarray $excludes$root_relative) {
  36.         $this->blog_url   = $blog_url;
  37.         $this->cdn_url    = $cdn_url;
  38.         $this->include_dirs  = $include_dirs;
  39.         $this->excludes   = $excludes;
  40.         $this->rootrelative  = $root_relative;
  41.     }
  42.     protected function exclude_single(&$match) {
  43.         foreach ($this->excludes as $badword) {
  44.             if (stristr($match$badword) != false) {
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50.     protected function rewrite_single(&$match) {
  51.         if ($this->exclude_single($match[0])) {
  52.             return $match[0];
  53.         } else {
  54.             if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
  55.                 return str_replace($this->blog_url, $this->cdn_url, $match[0]);
  56.             } else {
  57.                 return $this->cdn_url . $match[0];
  58.             }
  59.         }
  60.     }
  61.     protected function include_dirs_to_pattern() {
  62.         $input = explode(',', $this->include_dirs);
  63.         if ($this->include_dirs ==  || count($input) < 1) {
  64.             return 'wp\-content|wp\-includes';
  65.         } else {
  66.             return implode('|', array_map('quotemeta', array_map('trim', $input)));
  67.         }
  68.     }
  69.     public function rewrite(&$content) {
  70.         $dirs = $this->include_dirs_to_pattern();
  71.         $regex = '#(?<=[(\"\'])';
  72.         $regex .= $this->rootrelative ? ('(?:'.quotemeta($this->blog_url).')?') : quotemeta($this->blog_url);
  73.         $regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
  74.         return preg_replace_callback($regexarray(&$this, 'rewrite_single'), $content);
  75.     }
  76. }

 

推荐阅读
  • 以下仅供学习使用以及纪念之用,已过时,将不再继续鼓捣,请知悉。因能力有限,将它们弄出来后修改时都得调试半天,日后随缘上香。任何事物的成长都需要沉淀,不然就会成以下这些一样的结果。继续在另一个条漫长的转型不归路上走着…走着……以下仍然可以站内搜索相关简介:001、DNSP...
  • 插件截图:插件简介:TleUCenterForWordpress是一个用户中心插件,放置于前台网页的左下角,供用户登陆/管理只用,使用邮箱验证码登陆,登陆之后在使用TleWeiboForWordPressV2.0微博主题的情况下,可以显示微博列表、文章列表、评论列表,也可...
  • 插件截图:插件介绍:TleLiveCtrlForWordpress是一个基于Kplayer的直播遥控器插件,也可以叫做KplayerForWordPress插件,支持多平台直播推流,进行积分点播、查询、跳过等功能,支持Payjs微信、支付宝支付,是一个可以24小时直播推流的...
  • 主题截图:主题简介:一款Wordpress版本的TleWeiboForWordPress电脑/手机版微博主题使用方法:将本主题里的所有文件放在您网站目录的wp-content/themes内,注意文件夹名字必须为TleWeibo或TleWeiboWap。...
  • 插件简介:即时聊天插件为WordPress站长及用户提供即时聊天功能,前台环信即时聊天需要配合个人中心插件,暂不支持手机端。在支持手机端的路上,因为万恶的360网站卫士最近访问困难,导致停留了N久,最后啥也没动,就升级至V1.0.10了,不过仅仅支持了简单的手机端版本。使...

o p
Ú
>