WordPress怎么设置内链?文章内容标签自动加锚文本优化

前言:

我们都知道wordpress文章标签默认是中文,比如标签少的时候可以自己修改别名,比如拼音或者英文,但是有一个缺点就是同音词的时候不好区分,只能在别名后面加-1,-2 这样的区分,比如手机,收集,首级,遇到同音词的时候就很不好,展示方式看个人喜欢,除了中文展示会转码外,并没什么卵用,个人觉得用 ID 方式最好,比如 Discuz 的标签展示方式就最好。

其实最重要的是中文TAG标签会在某些浏览器被转义成乱码,个人觉得不好看!

将以下代码加在主题目录的 functions.php 中即可实现,如果想要ID以html结尾就更改return home_url('/tag/'.$term->term_id);成

return home_url('/tag/'.$term->term_id.'.html');即可。

注意:添加代码后需要进入后台重新保存一下固定链接。

/* 标签以id方式展示*/
 
add_action('generate_rewrite_rules','tag_rewrite_rules');
 
add_filter('term_link','tag_term_link',10,3);
 
add_action('query_vars', 'tag_query_vars');
 
function tag_rewrite_rules($wp_rewrite){
 
$new_rules = array(
 
'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
 
'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
 
'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true',
 
'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
 
'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]',
 
);
 
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
 
}
function tag_term_link($link,$term,$taxonomy){
 
if($taxonomy=='post_tag'){
 
return home_url('/tag/'.$term->term_id);
 
}
return $link;
}
function tag_query_vars($public_query_vars){
 
$public_query_vars[] = 'tag_id';
 
return $public_query_vars;
 
}

添加代码后链接样式

显示于标签相同的所有标签列代码:同意是代码加在主题目录的 functions.php 中即可实现

/* 自动为文章内的标签添加内链 */
$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 1; //一篇文章中同一个标签最多自动链接几次
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$cleankeyword = stripslashes($keyword);
$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('【查看含有[%s]标签的文章】'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}
add_filter('the_content','tag_link',1);

另一个:

//WordPress文章关键词自动内链
function tag_sort($a, $b){
  if ( $a->name == $b->name ) return 0;
  return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tag_link($content){
  $match_num_from = 1;	//一个标签少于几次不链接
  $match_num_to = 1;	//一个标签最多链接几次
  $posttags = get_the_tags();
  if ($posttags) {
    usort($posttags, "tag_sort");
    foreach($posttags as $tag) {
      $link = get_tag_link($tag->term_id);
      $keyword = $tag->name;
      //链接代码
      $cleankeyword = stripslashes($keyword);
      $url = "".addcslashes($cleankeyword, '$')."";
      $limit = rand($match_num_from,$match_num_to);
      //不链接代码
      $content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
      $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
      $cleankeyword = preg_quote($cleankeyword,'\'');
      $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?))\'s' . $case;
      $content = preg_replace($regEx,$url,$content,$limit);
      $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
    }
  }
  return $content;
}
add_filter('the_content','tag_link',1);

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。