百度SEO的普通收录好像现在每条只能推10个链接,我们通过在主题的functions.php文件中添加代码来实现定时推送的功能。
代码会先获取当前系统的所有文章,然后随机取出10个去推送,并把结果存到数据库,之后可以再取出来显示记录
//定时运行提交链接到百度/每天10条 // 注册一个WP-Cron事件 function custom_cron_job() { if ( ! wp_next_scheduled( 'custom_cron_action' ) ) { wp_schedule_event( time(), 'daily', 'custom_cron_action' ); } } add_action( 'wp', 'custom_cron_job' ); // 执行的代码 function custom_cron_function() { $urls = array(); $args = array( 'post_type' => 'post', 'posts_per_page' => -1, ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $urls[] = get_permalink(); } } wp_reset_postdata(); $random_urls = array_rand($urls, 10); $selected_urls = array(); foreach ($random_urls as $index) { $selected_urls[] = $urls[$index]; } $api = '你的api链接'; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $selected_urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); $timestamp = current_time( 'mysql' ); // 获取当前时间戳 // 存储结果和运行时间在数据库中 $data = array( 'result' => $result, 'timestamp' => $timestamp, ); add_option( 'custom_cron_result_history', $data, '', 'no'); } add_action( 'custom_cron_action', 'custom_cron_function' );
可以使用下面的代码来显示推送的记录
反序列化还有些问题但凑合能看,时间戳也不对劲。
$history = get_option( 'custom_cron_result_history' );
if ( $history ) {
foreach ( $history as $serializedData ) {
$data = maybe_unserialize($serializedData); // 反序列化数据
if ($data !== false) {
// 输出反序列化后的内容
echo '<pre>';
var_dump($data); // 或者使用 print_r($data);
echo '</pre>';
} else {
// 解析失败的处理逻辑
echo "Unserialization failed for data: $serializedData";
}
}
}