WordPress教程 纯代码实现为子比主题添加百度是否收录功能 子比主题美化

前言:

最近在群里看到小伙们都想要在文章中添加百度是否收录功能,于是蓝米兔百度了一下代码,然后就添加到子比主题中了。

这代码感觉没啥用,百度收没收录的文章它都显示已收录!!!效果如本文章标题下面。

如何添加:

1、打开 /zibll/inc/functions/functions.php 在最后面添加以下代码:

/**
 * WordPress 添加百度是否收录功能
**/
function baidu_check($url,$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,'抱歉未找到该URL,您可直接访问') && !strpos($rs,'很抱歉,没有找到与') ){
            update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
            return 1;
        } else {
            return 0;
        }
    } else {
       return 1;
    }
}
function baidu_record() {
    global $wpdb;
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    if(baidu_check(get_permalink($post_id), $post_id ) == 1) {
        echo '<a 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;" 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、打开 /zibll/inc/functions/zib-single.php 搜索 <?php echo $posts_meta; ?> 在这其下一行添加以下代码:

<?php baidu_record(); ?>   //大概在257行添加这段代码
图片[1]-WordPress教程 纯代码实现为子比主题添加百度是否收录功能 子比主题美化-蓝米兔博客
© 版权声明
THE END
喜欢就支持一下吧
点赞2赞赏 分享
评论 抢沙发

请登录后发表评论