关于Wordpress下的Twenty Fifteen主题优化

前言

最近在忙着将博客给优化一下,期间也碰到了几个坑,简单的做一下记录

博客加载速度慢

一开始还很纳闷,为啥网站加载起来总是没有加载完全,去网上查了一下才知道,因为国内的网络环境问题,wordpress的官方主题直接照搬来用还是有些水土不服,一是因为Google字体无法被调用,二是因为Gravatar头像也被墙无法显示,而解决办法也很简单。

  1. 直接把Google字体给禁用了,找到主题文件夹下的functions.php文件,找到如下代码:
	 * Translators: If there are characters in your language that are not supported
	 * by Noto Sans, translate this to 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
		$fonts[] = 'Noto Sans:400italic,700italic,400,700';
	}

	/*
	 * Translators: If there are characters in your language that are not supported
	 * by Noto Serif, translate this to 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
		$fonts[] = 'Noto Serif:400italic,700italic,400,700';
	}

把里面的if ( ‘off’ != = _x( ‘on’,中的on改为off,然后保存

  1. 再将Gravatar头像直接采用调用ssl头像链接,在function.php文件中找到加入代码:
function get_ssl_avatar($avatar) {
   $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','
   ',$avatar);
   return $avatar;
}

反正在我做完之后,再来访问网站,速度有明显提升(注意加入文件中的代码所有符号都必须是英文的半角符号,否则函数将会不生效)。

博客首页显示全文

这个问题就在于,首页显示文章的全文,会显得多余且冗长,而首页应该是简洁的。
解决办法1:
在主题文件夹下的index.php文件中找到如下代码:

			<?php
			// Start the loop.
			while ( have_posts() ) : the_post();

				/*
				 * Include the Post-Format-specific template for the content.
				 * If you want to override this in a child theme, then include a file
				 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
				 */
				get_template_part( 'content', get_post_format() );

			// End the loop.
			endwhile;

将get_template_part( ‘content’, get_post_format() )中的content改为content-search,保存好再次刷新网站就可以看到文章是以摘要形式显示了
解决办法2:
第二种解决方法其实也大同小异,只是把要修改的文件换成了content.php,不过我用的是第一种方法,第二种并没有尝试,也就不再多说。

更改摘要显示字数

虽然经过上一步的修改,文章是以摘要形式显示了,不过显示的摘要字数太少,为了进一步优化,我们可以通过在functions.php找到如下代码:

function twentyfifteen_search_form_modify( $html ) {
	return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
}
add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );

在后面加上以下代码(200表示摘要显示的字数,可以自定义,一般为200-300):

function twenty_fifteen_excerpt_length( $length ) {
return 200;
}
add_filter( 'excerpt_length', 'twenty_fifteen_excerpt_length', 999 );

至此,问题算是解决了

参考链接

https://www.foxsem.com/wp/262/
http://www.zuoyunlai.com/twentyfifteenzhutixiugaiyouhuabanxiazai/

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值