菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

WordPress定时采集并发布知乎《每天60秒》内容

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:wordpress教程 wordpress采集 定时采集

WordPress定时采集发布知乎《每天60秒》的方法如下:首先,你需要安装一个名为“WP-AutoPost”的插件。然后,在插件设置中,选择“采集器”,并添加一个新的采集任务。在任务设置中,填写相关信息,如文章来源(知乎)、关键词(每天60秒)等。接下来,设置定时任务的时间间隔,例如每天或每周。最后,保存设置并启用插件。这样,插件就会按照你设定的时间间隔自动采集知乎上关于《每天60秒》的文章,并在你的WordPress网站上发布。

wordpress+宝塔面板定时采集发布知乎的《每天60秒读懂世界》,设置定时任务后,访问该文件即可自动创建分类,自动截图并发布当日60的文章。

wordpress知乎每天60秒接口代码

<?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='<imgclass="size-fullwp-image-156aligncenter"src="https://www.caijicaiji.com/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("SELECTtt.term_taxonomy_idfrom$wpdb->termstjoin$wpdb->term_taxonomyttont.term_id=tt.term_idwheret.name='每天60秒读懂世界'andtt.taxonomy='category'")->term_taxonomy_id;if(!$term_taxonomy_id){$wpdb->query("insertinto$wpdb->terms(name,slug,term_group)VALUES('每天60秒读懂世界','60miao','0')");$category_id=$wpdb->insert_id;$wpdb->query("insertinto$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("SELECTidfrom$wpdb->postswherepost_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->postsSETguid=$post_guidwhereid=$insertid");//插入文章和分类、标签、专题的关系$sql="INSERTINTO$wpdb->term_relationships(object_id,term_taxonomy_id,term_order)VALUES";foreach($post_tag_arras$key=>$value){$sql.="($insertid,$value,'0'),";}$wpdb->query(rtrim($sql,","));}

自动采集设置方法

1.把代码保存为60s.php

2.放到网站根目录

3.修改33行的“https://www.caijicaiji.com/60s/”链接为个人网站链接即可

4.在宝塔面板设置定时任务访问:www.xxxx.com/60s.php,xxxx为你的网址,当然你没用宝塔也可以尝试其它方法来定时发布。

wordpress定时采集发布知乎《每天60秒》

如果创建分类后在前台页面点击分类不显示分类下的文章,需要在后台随便找个分类重新编辑保存下分类即可。(不需要修改内容)

此代码会在根目录下创建名称为60s的目录用于存放图片。


评论
建站知识
建站知识
使用技巧
调试安装
运营推广