之前发现了一个博客每天定时发布《每天60秒读懂世界》,发现类似的内容好像很好收录,就采集了一段时间,效果确实不错,经常能达到分钟级收录,但是那个博客断更了!!!

下面的代码是昨天仔细研究wordpress文章相关表和字段后完成的,经测试凌晨1点新内容发布成功!现在免费分享给大家!

代码为需要支付5福币,在本站注册登录签到即可领取5福币进行下载。

注意

  • 此代码会在/wp-content/uploads目录下创建名称为60s的目录用于存放图片。
  • 此代码会自动创建文章分类(每天60秒读懂世界)、创建标签(每天60秒读懂世界/每天60秒/60秒/读懂世界/每天六十秒)
  • 如果创建分类后在前台页面点击分类不显示分类下的文章,需要在后台重新编辑保存下分类(不需要修改内容)
  • WP Crontrol插件需要先随便创建一个任务然后去修改任务才能在修改事件类型为PHP cron 事件时添加PHP代码

使用教程

1.下载WP Crontrol插件,此插件用于发布定时任务,下载地址 👉 https://wordpress.org/plugins/wp-crontrol/

wordpress定时发布《每天60秒读懂世界》插图

2.将以下代码添加到任务中,注意插件相关的配置信息

wordpress定时发布《每天60秒读懂世界》插图1

 隐藏内容

关于API

global $wpdb;
date_default_timezone_set('PRC');
$post_tag_arr = array();
//先检查文章分类是否存在
$term_taxonomy_id = $wpdb->get_row("SELECT tt.term_taxonomy_id from $wpdb->terms t join $wpdb->term_taxonomy tt on t.term_id = tt.term_id where t.name = '每天60秒读懂世界' and tt.taxonomy = 'category' ")->term_taxonomy_id;
if(!$term_taxonomy_id){
    $wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('每天60秒读懂世界','60s','0')");
    $category_id = $wpdb->insert_id;
    $wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($category_id,'category','','0','1')");
    $term_taxonomy_id = $wpdb->insert_id;
}
$post_tag_arr[] = $term_taxonomy_id;

//检查文章标签是否存在 key=name value=slug
$terms_arr = array(
    "每天60秒读懂世界" => "mt60sddsj",
    "每天60秒" => "mt60s",
    "60秒" => "60m",
    "读懂世界" => "ddsj",
    "每天六十秒" => "mt60s"
);
foreach ($terms_arr as $key => $value){
    $term_taxonomy_id = $wpdb->get_row("SELECT tt.term_taxonomy_id from $wpdb->terms t join $wpdb->term_taxonomy tt on t.term_id = tt.term_id where t.name = '$key' and tt.taxonomy = 'post_tag' ")->term_taxonomy_id;

    if(!$term_taxonomy_id){
        $wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('$key','$value','0')");
        $post_tag = $wpdb->insert_id;
        $wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($post_tag,'post_tag','','0','1')");
        $term_taxonomy_id = $wpdb->insert_id;
    }
    $post_tag_arr[] = $term_taxonomy_id;
}


//解析接口数据
$post = json_decode(file_get_contents("http://www.fuym.cn/api/60s.php"),true);


//标题
$title = $post['title'];
//标题存在则不插入
$posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title = '$title' ");
if(!$posts){
    $FileName = "wp-content/uploads/60s/".strtotime(date("Y-m-d"),time()) . ".png";
    //查看图片是否存在
    if(!file_exists($FileName)){
        $dir = "wp-content/uploads/60s";
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        
        $url = $post['img'];
        $img = $FileName;
        file_put_contents($img, file_get_contents($url));
    }
    $url = "//".$_SERVER['SERVER_NAME']."/".$FileName;
    $newpost = "<img class='aligncenter' src='$url' width='900' >".$post['content'];

    $now        = current_time( 'mysql' );
    $now_gmt    = current_time( 'mysql', 1 );
    $wpdb->insert(
        $wpdb->posts,
        array(
            'post_author'           => 1,
            'post_date'             => $now,
            'post_date_gmt'         => $now_gmt,
            'post_content'          => $newpost,
            'post_title'            => $title,
            'post_excerpt'          => '',
            'post_status'           => 'publish',
            'comment_status'        => 'open',
            'ping_status'           => 'open',
            'post_password'         => '',
            'post_name'             => $title,
            'to_ping'               => '',
            'pinged'                => '',
            'post_modified'         => $now,
            'post_modified_gmt'     => $now_gmt,
            'post_content_filtered' => '',
            'post_parent'           => '0',
            'guid'                  => '',//文章链接 插入后修改
            'menu_order'            => '0',
            'post_type'             => 'post',
            'post_mime_type'        => '',
            'comment_count'         => '0',

        )
    );
    $insertid = $wpdb->insert_id;
    $post_guid = get_option( 'home' ) . '/?p='.$insertid;
    $wpdb->query(" UPDATE $wpdb->posts SET guid=$post_guid where id = $insertid ");

    //插入文章和分类、标签、专题的关系
    $sql = " INSERT INTO $wpdb->term_relationships (object_id,term_taxonomy_id,term_order) VALUES ";
    foreach ($post_tag_arr as $key => $value){
        $sql .= "($insertid, $value, '0'),";
    }
    $wpdb->query(rtrim($sql, ","));

}

3.完成!通过定时任务每天凌晨一点会自动发布一篇最新的《每天60秒读懂世界》相关内容!可以点击Run Now来立即执行任务,然后查看您的文章列表中是否新增了今天的每日60秒。

wordpress定时发布《每天60秒读懂世界》插图2

转自:http://www.fuym.cn/162.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注