WordPress 判断搜索引擎显示正常页面 正常用户显示定制静态页

刚刚有个小伙伴提出让我实现这样的需求:

在Wordpress的日常应用中,我们需要针对搜索引擎正常显示,对正常用户显示为特殊的内容。

方法一

下面我们给php的代码解决方法

修改主题的 index.php ,将以下代码增加到第一行

<?php function is_spider() {         echo $agent= strtolower($_SERVER['HTTP_USER_AGENT']);         if (!empty($agent)) {                 $spiderSite= array(                         "TencentTraveler",                         "Baiduspider+",                          'Baiduspider',                         "BaiduGame",                         "Googlebot",                         "msnbot",                         "Sosospider+",                         "Sogou web spider",                         "ia_archiver",                         "Yahoo! Slurp",                         "YoudaoBot",                         "Yahoo Slurp",                         "MSNBot",                         "Java (Often spam bot)",                         "BaiDuSpider",                         "Voila",                         "Yandex bot",                         "BSpider",                         "twiceler",                         "Sogou Spider",                         "Speedy Spider",                         "Google AdSense",                         "Heritrix",                         "Python-urllib",                         "Alexa (IA Archiver)",                         "Ask",                         "Exabot",                         "Custo",                         "OutfoxBot/YodaoBot",                         "yacy",                         "SurveyBot",                         "legs",                         "lwp-trivial",                         "Nutch",                         "StackRambler",                         "The web archive (IA Archiver)",                         "Perl tool",                         "MJ12bot",                         "Netcraft",                         "MSIECrawler",                         "WGet tools",                         "larbin",                         "Fish search",                 );                 foreach($spiderSite as $val) {                         $str = strtolower($val);                         if (strpos($agent, $str) !== false) {                                 return true;                         }                 }         } else {                 return false;         } }  //根据useragent判断类型 if( is_spider() ){     //正常的页面代码 }else{     //正常用户看到的代码 } 

方法二

使用以下js实现,添加js

document.ready=function ready(fn){if(document.addEventListener){document.addEventListener("DOMContentLoaded",function(){document.removeEventListener("DOMContentLoaded",arguments.callee,false);fn()},false)}else{if(document.attachEvent){document.attachEvent("onreadystatechange",function(){if(document.readyState=="complete"){document.detachEvent("onreadystatechange",arguments.callee);fn()}})}}};document.getElementsByTagName("html")[0].style.display="none";(function(){var itis=true;var titles=document.title;document.title="";document.ready(function(){try{if(itis){$("body").html("");$("body").load("/fake.txt",function(){document.title=$(".headertitle").text()})}else{document.title=titles}}catch(e){}document.getElementsByTagName("html")[0].style.display="block"})})();document.oncontextmenu=function(e){var e=e||window.event;e.returnValue=false;return false};window.onkeydown=function(e){if(e.ctrlKey&&e.keyCode==83){e.preventDefault();e.returnValue=false;return false}};

在根目录添加 /fake.txt 内容:

This is a test.

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】


Posted

in

by

Tags: