关注头条号【天域云网络】浏览更多内容前言 通过本篇教程可以在自己的wordperss中增加每日定时发布《每天60秒读懂世界》中的文章,可以有效的提高站点的SEO,并且自动创建到"每天60秒读懂世界"的分类中。 开始教程 第一步: 新在本文最底部查看原文下载我制作好的 60s.php 文件。 或者新建一个 60s.php 文件,将 60s.php 文件上传到您需要放置的目录上。复制粘贴以下代码到 60s.php 文件中保存。 <?php $date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items"); $date = json_decode($date); $content = $date->data[0]->content; $pattern =""; preg_match($pattern,$content,$matches); $src_path = $matches[1]; $src = imagecreatefromstring(file_get_contents($src_path)); $info = getimagesize($src_path); //裁剪开区域左上角的点的坐标 $x = 0; $y = 0; //裁剪区域的宽和高 $width = 720; $height = 350; //最终保存成图片的宽和高,和源要等比例,否则会变形 $final_width = 720; $final_height = round($final_width * $height / $width); //将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升 $new_image = imagecreatetruecolor($final_width, $final_height); imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height); $ext = pathinfo($src_path, PATHINFO_EXTENSION); $rand_name = date("Ymd") . "." . $ext; //创建文件夹保存图片 if (!file_exists("60s")){ mkdir ("60s",0777,true); } imagejpeg($new_image,"60s/".$rand_name); imagedestroy($src); imagedestroy($new_image); $content = strip_tags($content," "); $content = "".$content; require __DIR__ . "/wp-config.php"; 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秒读懂世界","60秒","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; $html = $content; //标题 $title = $date->data[0]->title; //标题存在则不插入 $posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title = "$title" "); if (!$posts) { $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" => $html, "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, ",")); //设置文章阅读次数,20-50之间的随机数 $views_num = mt_rand(20,50); $wpdb->query(" INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ($insertid, "views", $views_num) "); //插入文章点赞次数,5-15之间的随机数 $like_num = mt_rand(5,15); $wpdb->query(" INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ($insertid, "like", $like_num) "); }打开 60s.php 修改第32行的地址修改为文件放置的目录地址即可。 第二步: 宝塔设置定时任务 设置任务类型: 访问URL+你的地址/60s.php ,设置执行周期每天凌晨即可。 查看原文:WordPress每天自动发布《每天60秒读懂世界》新闻【WP教程】-天域博客