WordPress主题给文章增加百度是否已收录的功能

 3454

WordPress主题给文章增加百度是否已收录的功能,这个功能添加起来简单,首先编辑主题目录的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().'">百度已收录</a>';
    } else {
        echo '<a style="color:red;" rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>';
    }
}

第二步,编辑文章模板(一般是single.php,begin5.2是/template/content.php),在合适的位置添加如下代码并保存:如果是begin主题的话可以参考begin5.2文件目录详情

<?php baidu_record(); ?>

若以上步骤都未出错,那现在访问文章页面就能看到百度是否已收录的效果了。你也可以把这段代码放在首页或喜欢的地方。

其实这个方法大家可以改造一下,加入到自己的网站系统中,都是一样的。




本文网址:https://www.zztuku.com/detail-7963.html
站长图库 - WordPress主题给文章增加百度是否已收录的功能
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐