本文讲的是WordPress 网站获取所有文章链接,并向百度搜索主动推送所有url,使用的方法是调用推送接口,用php推送。
第一步、获取所有已发布文章(ID)。
代码如下:
- <?php
- include ( “wp-config.php” ) ;
- require_once (ABSPATH.’wp-blog-header.php’);
- global $wpdb;
- //填你的前缀
- $qianzui = “https://xyy1.com/”;
- //填你的后缀
- $houzui = “.html”;
- //获取所有文章 url
- $sql=“SELECT ID FROM wp_posts WHERE post_type = ‘post’ AND post_status = ‘publish’ ORDER BY ID DESC “;
- $myrows = $wpdb->get_results($sql);
- $urls = array();
- foreach ($myrows as $b) {
- $id = $b->ID;
- $urls[] =$qianzui.$id.$houzui;
- }
- //获取所有tag url
- $tg_terms_sql=“SELECT slug FROM wp_terms ORDER BY `tg_terms`.`term_id` DESC “;
- $my_tags = $wpdb->get_results($tg_terms_sql);
- $tag_urls = array();
- foreach ($my_tags as $a) {
- $tag = $a->slug;
- $tag_urls[] =$qianzui.“tag/”.$tag;
- }
- //合并文章URL和TAG url
- $urls = array_merge($urls,$tag_urls);
- //推送接口 将所有链接主动推送到百度
- $api = ‘http://data.zz.baidu.com/urls?site=https://xyy1.com&token=wE2bcdcomcHyLe’;
- $ch = curl_init();
- $options = array(
- CURLOPT_URL => $api,
- CURLOPT_POST => true,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POSTFIELDS => implode(“n”, $urls),
- CURLOPT_HTTPHEADER => array(‘Content-Type: text/plain’),
- );
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- echo $result;
- ?>
发表回复