64、wordpress文章中自动插入相近的关键词内链-文公子副业-兼职-网赚-赚钱项目
欢迎您光临本站,秉承服务宗旨,履行"站长"责任,销售只是起点,服务永无止境!

64、wordpress文章中自动插入相近的关键词内链

作者 : 文公子副业 本文共2450个字,预计阅读时间需要7分钟 发布时间: 2023-06-18 共116人阅读

使用场景:文章中插入关键词内链有助于seo,可以将在文章中出现的关键词自动转换为链接,并且仅在关键词与文章标题相似的情况下插入链接。一个个添加又过于麻烦。

代码添加到主题文件夹下 functions.php 文件中:

function add_internal_links($content) {
global $post;
$keywords = get_keywords_from_title($post->post_title);
$internal_links = get_internal_links();
foreach ($keywords as $keyword) {
$link = '';
foreach ($internal_links as $internal_link) {
if (strpos(strtolower($internal_link['title']), strtolower($keyword)) !== false) {
$link = $internal_link['url'];
break;
}
}
 
 
 
 
if (!empty($link)) {
$link_html = '<a href="' . $link . '">' . $keyword . '</a>';
$content = str_ireplace($keyword, $link_html, $content);
}
}
 
 
 
 
return $content;
}
 
function get_keywords_from_title($title) {
$title_words = explode(' ', $title);
$keywords = array();
foreach ($title_words as $word) {
if (strlen($word) > 3) {
$keywords[] = $word;
}
}
 
 
 
 
return $keywords;
}
 
function get_internal_links() {
$internal_links = array();
$args = array(
'post_type' => array('post', 'page'),
'post_status' => 'publish',
'posts_per_page' => -1
);
 
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$internal_links[] = array(
'title' => get_the_title(),
'url' => get_permalink()
);
}
 
 
 
 
wp_reset_postdata();
}
 
return $internal_links;
}
 
add_filter('the_content', 'add_internal_links');

或者

//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 = "<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[^>]+>)(.*)<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 . ')(?!(([^<>]*?)>)|([^>]*?</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);

还有也可以使用WP Keyword Link插件

函数会在文章内容被渲染之前执行,它首先获取当前文章标题中的关键词,然后获取所有已发布的文章和页面的标题和链接,并检查其中是否有与关键词相似的标题。如果有,它会将关键词转换为内部链接。最后,它返回更新后的文章内容。

具体需要根据自己的站点需要进行修改。

文公子副业是文公子创盟旗下的副业,兼职,网赚网站。
文公子副业-兼职-网赚-赚钱项目 » 64、wordpress文章中自动插入相近的关键词内链

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

发表回复

开通VIP 享更多特权,建议使用QQ登录