
WordPress在SEO
方面实在是做的不够, 以至于关键词都不支持. 今天介绍一个简单的方法, 实现文章题目自动生成关键字.
工具准备
- Pullword api 一个永久免费的可自定义的中文在线分词API. 感谢!
开工
找到wp-includes/general-template.php
文件, 原本为:
/**
* Displays title tag with content.
*
* @ignore
* @since 4.1.0
* @since 4.4.0 Improved title output replaced `wp_title()`.
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
修改为:
/**
* Displays title tag with content.
*
* @ignore
* @since 4.1.0
* @since 4.4.0 Improved title output replaced `wp_title()`.
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
// 文章标题
$title = wp_get_document_title();
// 先展示<title/>
echo '<title>' . $title . '</title>' . "\n";
// 标题格式会带有站点信息, 这里将其截去.
// 大家在使用的时候需要根据自己的情况修改`-19`这个参数
$title = trim(substr($title, 0, -19));
// 中文标题中包含的英文一般都是完整的关键词.
// 而pullword这个分词工具对英文的效果不太好, 这里先将英文关键词提取出来
preg_match_all('/[a-zA-Z]+[0-9a-zA-Z]+/', $title, $matches);
$engKeywords = join(",", $matches[0]);
// 调用pullword api, param1为最低概率阈值, param2为debug模式开关(0=关闭)
$url='http://api.pullword.com/get.php?source='. urlencode($title) .'¶m1=0.9¶m2=0';
$html = trim(file_get_contents($url));
if ($html == "error"){
echo '<meta name="keywords" content="' . $engKeywords. '" />';
return;
}
// 将分词得到的关键词拼接到tag中
$keywords = str_replace("\n",",",$html);
if ($keywords != ""){
if ($engKeywords != ""){
$keywords = $keywords . "," . $engKeywords;
}
}
// 展示keyword <meta>
echo '<meta name="keywords" content="' . $keywords . '" />';
}
好像没有效果!