WordPress上添加显示百度是否收录

编辑主题目录的functions.php文件,在最后一个 ?> 标签前新添如下代码并保存(如没有 ?> 标签,则直接加到最后面。修改前一定要备份数据哦!)

//百度收录展示
function baidu_check($url){
    global $wpdb;
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    $baidu_record  = get_post_meta($post_id,'baidu_record',true);
    if( $baidu_record != 1){
        $url='http://www.baidu.com/s?wd='.$url;
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $rs=curl_exec($curl);
        curl_close($curl);
        if(!strpos($rs,'没有找到')){
            if( $baidu_record == 0){
                update_post_meta($post_id, 'baidu_record', 1);
            } else {
                add_post_meta($post_id, 'baidu_record', 1, true);
            }    
                return 1;
        } else {
            if( $baidu_record == false){
                add_post_meta($post_id, 'baidu_record', 0, true);
            }    
            return 0;
        }
    } else {
       return 1;
    }
}
function baidu_record() {
    if(baidu_check(get_permalink()) == 1) {
        echo '<a style="color:green;font-size:12px;float: right;" target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'"><i class="fa fa-paw fa-lx"></i>百度已收录</a>';
   } else {
        echo '<a style="color:red;font-size:12px;float: right;" rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'"><i class="fa fa-paw fa-lx"></i>百度未收录</a>';
   }
}

2、最后添加一个钩子,编辑文章模板(孟坤主题:wp-content/themes/mkBlog/inc/article-head.php
),在合适的位置添加如下代码并保存:

<?php baidu_record(); ?>

原创文章,作者:陌涛,如若转载,请注明出处:https://imotao.com/2561.html

(0)
陌涛的头像陌涛
上一篇 2020年5月15日 上午11:47
下一篇 2020年5月16日 上午10:35

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

评论列表(1条)

  • 44756539的头像
    44756539 2020年8月30日 下午12:12

    你好,添加之后所有的网页都是显示已收录,这是为啥[aru_2]