文章目录[隐藏]
方法很简单,只需要两步操作就可以实现
第一步:修改functions.php文件
首先打开你的主题目录下的functions.php文件,我们需要将以下代码添加到你主题的functions.php文件中,部分主题可能禁止编辑这个文件,不过主题作者应该会提供另一个文件方便大家对主题的修改,比如functions_z.php,具体文件可咨询主题作者。
/**
* WordPress 添加百度是否收录功能
* https://www.mengtg. cn/
**/
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文件
找到主题文件目录下的single.php文件,single.php是主题的文章页面,一般我们将这个功能添加到这里可以方便访客实时查看到此篇文章是否被百度收录,你可以将下面的短代码添加至你希望放置的位置。
<?php baidu_record(); ?>
对于使用DUX主题的,是把代码添加到以下位置,大家可以参考参考一下!
<span class="item"><?php echo _get_post_comments() ?></span>
<span class="item"><?php baidu_record(); ?></span>
<span class="item"><?php edit_post_link('[编辑]'); ?></span>