将下面的代码添加到主题的 functions.php
function ribs_baidu_check($url) {
global $wpdb;
$post_id = (null === $post_id) ? get_the_ID() : $post_id;
$baidu_record = get_post_meta($post_id, 'ribs_baidu_record', true);
if ($baidu_record != '收录') {
$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 == '未收录') {
update_post_meta($post_id, 'ribs_baidu_record', '收录');
} else {
add_post_meta($post_id, 'ribs_baidu_record', '收录', true);
}
return '收录';
} else {
if ($baidu_record == false) {
add_post_meta($post_id, 'ribs_baidu_record', '未收录', true);
}
return '未收录';
}
} else {
return '收录';
}
}
function ribs_baidu_record() {
if (ribs_baidu_check(get_permalink()) == '收录') {
echo '<a target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd=' . get_the_title() . '" >收录</a>';
} else {
echo '<a rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename=' . get_permalink() . '" >未收录</a>';
}
}
在网页适当位置添加
<?php ribs_baidu_record(); ?>
你不希望所有人看到的话,加上一个判断登陆人权限的代码
<?php if (current_user_can('level_10')){ribs_baidu_record();}; ?>
想要在后台,看见的话,在 functions.php
里加
//后台文章管理列表显示收录情况
add_filter('manage_posts_columns', 'my_add_posts_columns', 5);
add_action('manage_posts_custom_column', 'my_custom_posts_columns', 5, 2);
function my_add_posts_columns($defaults) {
$defaults['ribs_baidu_record'] = '百度收录';
return $defaults;
}
function my_custom_posts_columns($column_name, $id) {
if ($column_name === 'ribs_baidu_record') {
echo get_post_meta(get_the_ID(), 'ribs_baidu_record', true);
}
}