[wm_blue]每日六十秒读懂世界是一款早报生成器,相信不少朋友都在其他地方看大过每天60s新闻,这里就教大家通过wordpress生成一张由今日热点新闻组成的早报图片,轻轻一点即可保存至手机上。[/wm_blue]
效果截图:
设置教程
2.1 新闻自动发布代码:将以下代码保存为php文件,可命名为60.php或者你想命名的名字,并放在网站根目录或者任意目录下
[pwd_protected_post key=”mt60s”]
<?php
$date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items");
$date = json_decode($date);
$content = $date->data[0]->content;
$content = preg_replace('/(<a.*?>[\s\S]*?<\/a>)/','',$content);
$pattern ='<img.*?src="(.*?)">';
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,'<p>');
$content = '<img class="size-full wp-image-156 aligncenter" src="https://momeis.net/60s/'.$rand_name.'" alt="" width="720" height="350" />'.$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秒读懂世界','60miao','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' => 21,
'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' => 'newsflashes',
'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, ","));
}
[/pwd_protected_post]
2.2 WordPress集成自动发布新闻
首先修改php里面的网站链接为你自己的,32行左右,原代码为
$content = '<img class="size-full wp-image-156 aligncenter" src="https://momeis.net/60s/'.$rand_name.'" alt="" width="720" height="350" />'.$content;
教程到这里大致就已经完成了,不过还差一个定时执行,这里我们可以通过宝塔计划任务来设置执行时间
这里的URL地址事你上传的PHP文件路径
2.3 说明
该源码会自动创建文章和分类,图片存放位置也是单独的文件夹,后期删除很方便
2.4 7B2主题进阶设置-设置作者与快讯
设置作者:建议添加一位新用户,设置发布快讯资格,作为新闻发布机器人,author处
设置发布文章类型,建议同本站一样,放在快讯
post-文章
circle-圈子
document-文档
执行后没反应的,看看wp用户作者权限有没有给到,建议先直接设置作者为站长测试;
另外执行后需要一定时间才会有,代码经过多位站长测试,均无问题。