從運營網(wǎng)站策略上講,如果我們內容關聯(lián)度和內鏈不夠,也會影響網(wǎng)站在用戶體驗和搜索引擎的排名。這里,我們會通過一些技術手段來彌補我們沒有設置的內容內鏈,比如我們會通過WORDPRESS添加TAG自動內鏈提高內鏈的契合度。這里,我們無需用插件,直接用代碼可以實現(xiàn)。
$match_num_from = 10;//一篇文章內標簽自動添加內鏈的數(shù)量,建議不超過20個 $match_num_to = 2;//一篇文章內重復標簽自動添加內鏈的數(shù)量,建議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);
我們可以修改開頭兩行的數(shù)值調節(jié)內鏈的數(shù)量和重復次數(shù)。
評論