无插件实现WordPress自动获取关键字(keywords)、描述(description)

如果我们有使用原生态的WordPress程序,在一些用户体验和搜索引擎体验设置是不够理想的,不过如果我们选择一些主题中可能会自带这些功能。比如标题自定义、关键字和描述的设置,我们一般还可以使用插件自动获取。

但是,这个有一个问题,比如无插件的那方式是直接在主题头部页面修改的,但是有些头部页面是嵌套在定义脚本中的有些时候还找不到,所以我们还需要一个更为通用的方法。这篇稳重,老蒋要分享的是直接定义在主题functions.php文件中的。基本上适合所有主题。

百码云就没有没有利用插件来实现这一功能的,下面就这种简单的方式也大家分享,

打开你的wourdpress头部文件header.php,加入以下代码:

<?php if (is_home()){ $keywords = "网站源码,小程序源码,源码资源,小程序,微信小程序,PHP源码,公众号应用,html模板,应用模块,百码云";//换成你的网站的关键词 $description = "百码云(microeco.net)旨为互联网从业者提供精品源码下载和建站资源的交流分享,包括网站源码、微信应用、小程序源码、公众号应用、微赞模块、商城系统、资讯教程、工具软件等站长资源。";//换成你的网站的摘要描述 } elseif (is_single()){ $tags = wp_get_post_tags($post->ID);
	foreach ($tags as $tag){
		$keywords = $keywords.$tag->name.",";
	}
	$keywords = rtrim($keywords, ', ');
	if($post->post_excerpt){
		$description = $post->post_excerpt;
	}else{
		$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);
	}
}
elseif (is_page()){
	$keywords = get_post_meta($post->ID, "keywords", true);
	$description = get_post_meta($post->ID, "description", true);
}
elseif (is_category()){
	$keywords = single_cat_title('', false);
	$description = category_description();
}
elseif (is_tag()){
	$keywords = single_tag_title('', false);
	$description = tag_description();
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>

百码云就没有没有利用插件来实现这一功能的,下面就这种简单的方式也大家分享,

打开你的wourdpress头部文件header.php,加入以下代码:

<?php if (is_home()){ $keywords = "网站源码,小程序源码,源码资源,小程序,微信小程序,PHP源码,公众号应用,html模板,应用模块,百码云";//换成你的网站的关键词 $description = "百码云(microeco.net)旨为互联网从业者提供精品源码下载和建站资源的交流分享,包括网站源码、微信应用、小程序源码、公众号应用、微赞模块、商城系统、资讯教程、工具软件等站长资源。";//换成你的网站的摘要描述 } elseif (is_single()){ $tags = wp_get_post_tags($post->ID);
	foreach ($tags as $tag){
		$keywords = $keywords.$tag->name.",";
	}
	$keywords = rtrim($keywords, ', ');
	if($post->post_excerpt){
		$description = $post->post_excerpt;
	}else{
		$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);
	}
}
elseif (is_page()){
	$keywords = get_post_meta($post->ID, "keywords", true);
	$description = get_post_meta($post->ID, "description", true);
}
elseif (is_category()){
	$keywords = single_cat_title('', false);
	$description = category_description();
}
elseif (is_tag()){
	$keywords = single_tag_title('', false);
	$description = tag_description();
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>
本站部分文章资源来源于互联网,仅供学习交流,如若要商用,请购买正版!
若不听劝告,网友造成出现一切后果,与本站本人无关
本站有些资源未经测试,请注意网络安全,本站不对下载的资源造成的后果负责
免责声明
作者:昼白
转载请注明来源:https://www.2bcnm.com/2302.htm
THE END
分享
二维码
打赏
海报
无插件实现WordPress自动获取关键字(keywords)、描述(description)
如果我们有使用原生态的WordPress程序,在一些用户体验和搜索引擎体验设置是不够理想的,不过如果我们选择一些主题中可能会自带这些功能。比如标题自定义、关……
<<上一篇
下一篇>>