侧边栏壁纸
博主昵称

wordpress每天60秒,一篇小文章 🍂

wordpress每天60秒,一篇小文章
  • 文章发布于2023-05-27 12:14:22,共388字,阅读内容所需的预计时间2分钟。 本文总阅读量

新建php文件,拷贝如下内容,把里面的名称什么的换成自己的网站,每天做个定时任务自动调用一下就行。

自己做个记录。如果有需要可以自行修改内容

  1data[0]->content;
  2
  3$pattern ='';
  4
  5preg_match($pattern,$content,$matches);
  6
  7$src_path = $matches[1];
  8
  9$src = imagecreatefromstring(file_get_contents($src_path));
 10
 11$info = getimagesize($src_path);
 12
 13//裁剪开区域左上角的点的坐标
 14
 15$x = 0;
 16
 17$y = 0;
 18
 19//裁剪区域的宽和高
 20
 21$width = 720;
 22
 23$height = 350;
 24
 25//最终保存成图片的宽和高,和源要等比例,否则会变形
 26
 27$final_width = 720;
 28
 29$final_height = round($final_width * $height / $width);
 30
 31//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
 32
 33$new_image = imagecreatetruecolor($final_width, $final_height);
 34
 35imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
 36
 37$ext = pathinfo($src_path, PATHINFO_EXTENSION);
 38
 39$rand_name = date("Ymd") . "." . $ext;
 40
 41//创建文件夹保存图片
 42
 43if (!file_exists("60s")){
 44
 45mkdir ("60s",0777,true);
 46
 47}
 48
 49imagejpeg($new_image,"60s/".$rand_name);
 50
 51imagedestroy($src);
 52
 53imagedestroy($new_image);
 54
 55$content = strip_tags($content,'');
 56
 57$content = ''.$content;
 58
 59require __DIR__ . '/wp-config.php';
 60
 61global $wpdb;
 62
 63date_default_timezone_set('PRC');
 64
 65$post_tag_arr = array();
 66
 67//先检查文章分类是否存在
 68
 69$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;
 70
 71if (!$term_taxonomy_id) {
 72
 73$wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('每天60秒','nevents','0')");
 74
 75$category_id = $wpdb->insert_id;
 76
 77$wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($category_id,'category','','0','1')");
 78
 79$term_taxonomy_id = $wpdb->insert_id;
 80
 81}
 82
 83$post_tag_arr[] = $term_taxonomy_id;
 84
 85
 86
 87
 88$html = $content;
 89
 90
 91
 92
 93//标题
 94
 95$title_t = $date->data[0]->title;
 96
 97$title = '酸奶带你看世界,今天是'.$title_t;
 98
 99//标题存在则不插入
100
101$posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title = '$title' ");
102
103if (!$posts) {
104
105$now = current_time('mysql');
106
107$now_gmt = current_time('mysql', 1);
108
109$wpdb->insert(
110
111$wpdb->posts,
112
113array(
114
115'post_author' => 1,
116
117'post_date' => $now,
118
119'post_date_gmt' => $now_gmt,
120
121'post_content' => $html,
122
123'post_title' => $title,
124
125'post_excerpt' => '',
126
127'post_status' => 'publish',
128
129'comment_status' => 'open',
130
131'ping_status' => 'open',
132
133'post_password' => '',
134
135'post_name' => $title,
136
137'to_ping' => '',
138
139'pinged' => '',
140
141'post_modified' => $now,
142
143'post_modified_gmt' => $now_gmt,
144
145'post_content_filtered' => '',
146
147'post_parent' => '0',
148
149'guid' => '',//文章链接 插入后修改
150
151'menu_order' => '0',
152
153'post_type' => 'post',
154
155'post_mime_type' => '',
156
157'comment_count' => '0',
158
159
160
161
162)
163
164);
165
166$insertid = $wpdb->insert_id;
167
168$post_guid = get_option('home') . '/?p=' . $insertid;
169
170$wpdb->query(" UPDATE $wpdb->posts SET guid=$post_guid where id = $insertid ");
171
172
173
174
175//插入文章和分类、标签、专题的关系
176
177$sql = " INSERT INTO $wpdb->term_relationships (object_id,term_taxonomy_id,term_order) VALUES ";
178
179foreach ($post_tag_arr as $key => $value) {
180
181$sql .= "($insertid, $value, '0'),";
182
183}
184
185$wpdb->query(rtrim($sql, ","));
186
187
188
189
190}
php
博主栏壁纸
博主头像 爱喝酸奶的我

懒人博客,随缘记录生活点滴

15 文章数
12 评论量
最新评论
标签云
    一言
    今天早上我告诉你我想你了,你没理我。今天中午我给你打电话,你不接,打第二个你就关机。晚上我在你公司楼下等你,你对我说的第一句话就是滚“滚,别烦我,别浪费时间了”,我真的好感动,你居然为我考虑了,怕我浪费时间。呜呜呜,这是我爱你的第74天。