用文章简介作为meta-description

2013-12-21 23:45:32

[网站记录 | Web Dev] , ,

wordpress 的SEO做得也不是太好吧,好在有很多关于SEO的插件,但插件都比较花哨,装得多也不好,于是乎自己就改了一下主题,在主题里面加了一段代码。

 

让wordpress使用文章简介作为meta的description

 

 

本来还想弄将Tags(标签)作为keywords的,但是后来想想Google好像不太看重Keywords,所以就不弄了,还是尽量减少运算量吧,其实也不知道为什么现在我的网站的内存消耗上升了,大概是文章多起来的缘故吧。

 

 

代码其实也很简单,在header.php里面head标签中加入以下代码就可以了

	<?php
		if (is_home()){
		    $description = "关注互联网前沿技术,Linux Nginx MySQL PHP架构应用技术的科技博客。";
			?><meta name="description" content="<?php echo $description;?>" /><?php
		} elseif (is_single()){
			$findme   = "<!--more-->";
			$pos = strpos($post->post_content, $findme);
		    $description = substr(strip_tags($post->post_content),0,$pos);
			$search  = array("\r\n", "\r", "\n", " ", "&nbsp;");
			$replace = array("", "", "", "", "");
			$description = str_replace($search, $replace, $description);
			?><meta name="description" content="<?php echo $description;?>" /><?php
		}
	?>

 

 

 

需要修改的是主页的默认description,可以改称你喜欢的内容。这段代码可以让主页和文章页面显示meta的description。

 

 

 

这篇博文发表在 网站记录 | Web Dev 目录下,标签为 , ,
如需引用,请使用链接:https://note.mc256.dev/?p=204

This article published in 网站记录 | Web Dev with tags , , .
Cite this page using this link:https://note.mc256.dev/?p=204

您的邮箱地址不会被公开,评论使用Gravatar头像。
Your email address will not be published. This blog is using Gravatar.

正在提交评论... Submitting ...
正在为您准备评论控件 Loading Comment Plugin
Copyright © 2013-2024 mc256. All Rights Reserved.
Powered by WordPress on top of a dual-stack k3s Cluster using JuiceFS.
Wordpress Theme Designed By mc256.
Encrypted By Let's Encrypt.  Hosted On Linode + OVH + AWS.
DNS Provided By Hostker.
Status Page by CloudFlare Worker.