文章展示
文章列表展示
一、WordPress
配置文件代码
复制文件,修改 32 行的网站域名改为自己的网站域名即可。
将改好的文件上传到网站的根目录。
在宝塔中添加一个计划任务,选择访问网址,添加网址:https://自己的网站/60s.php。
由于很多站长都添加了这个功能,网上出现了很多相似页面和内容,不利于搜索引擎收录,建议再修改一下文章分类目录的名称和别名。修改位置在第 38 和 40 行。
<?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 ='<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="http://idc.yanchuan.cc/60s/'.$rand_name.'" 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 = '每日读懂全世界' and tt.taxonomy = 'category' ")->term_taxonomy_id; if (!$term_taxonomy_id) { $wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('每日读懂全世界','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, ",")); }
也可以直接下载:http://idc.yanchuan.cc/wp-content/uploads/2022/12/1671243489-60miao.zip
https://langniao.lanzouf.com/iiXou0ivp8fi
设置定时访问任务
https://你的域名/60s.php(可以通过宝塔之间进行创建)
宝塔创建定时任务方法
找到宝塔的计划任务
任务类型选择访问url
url地址填写:https://你的域名/60s.php,名称可随便填写,执行时间可以是每天更新一次。比如:每天, 1点30分
没有使用宝塔的可以使用插件automatorwp
在WordPress后台安装automatorwp
点击Add New Automation
选择All users
选择add Actions
选择autormatorWP
现在是右侧
点击保存
每天0:0刷新访问
二、zblog在后台插件搜索60s看新闻,安装插件后,激活插件,拷贝插件给你的地址,再在宝塔或者WordPress插件中自动访问。
三、采用第三方api
申请ALAPI接口
这里需要用到接口有两个,一个是ALAPI的,另一个我也不知道是谁(没限制)!
Functions代码(添加到主题的Functions中)
如果小工具无法使用PHP代码的要添加以下代码(可以使用的请忽略):
//实现侧边栏文本工具运行PHP代码
add_filter('widget_text', 'php_text', 99); function php_text($text) { if (strpos($text, '<' . '?') !== false) { ob_start(); eval('?' . '>' . $text); $text = ob_get_contents(); ob_end_clean(); } return $text; }
剩下我们就添加代码了,把token换成自己的!里面用到的icon是我自己的,如果无法显示的话可以替换掉或者添加本站自用的,剩下的自己琢磨改成自己的数据吧!
<!--阿里图标库-->
<script src="//at.alicdn.com/t/font_3113039_ynmx4q6y0hi.js"></script>
//新闻60秒早报(添加到主题Functions中)
function newzaobao(){//60s
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://v2.alapi.cn/api/zaobao",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "token=您自己的token&format=json",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$a=json_decode($response,true);
$code=$a['code'];
curl_close($curl);
if(is_home()){
echo "<posts class='posts-item list ajax-item flex' style='padding: 0;margin: 0;box-shadow: none;border-radius: unset;'><div class='post-graphic'><div class='item-thumbnail'><a href='/sixs' title='新闻早早报' rel='noopener noreferrer'>";
if($code!='200'){
echo "<img width='100%' height='100%' class=' lazyloaded' src='https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2795843d-b24e-4ca8-8f8b-7449c17ce44f/7e0efd54-e8f2-468b-bdde-926fc96702c4.png' data-src='https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2795843d-b24e-4ca8-8f8b-7449c17ce44f/7e0efd54-e8f2-468b-bdde-926fc96702c4.png' alt='新闻早早报'>";
}else{
echo "<img width='100%' height='100%' class=' lazyloaded' src='" . $a['data']['head_image'] . "' data-src='" . $a['data']['head_image'] . "' alt='新闻早早报'>";
}
echo "</a><badge class='img-badge left jb-red' style='background-image: -webkit-linear-gradient(0deg,#3a8ee6 0%,#93c8ff 100%);'>日报</badge></div></div><div class='item-body flex xx flex1 jsb'><h2 class='item-heading'>";
echo "<a href='/sixs' class='title' title='新闻早早报' rel='noopener noreferrer'>";
echo "<span class='badge' style='display: inline-block; background-image: -webkit-linear-gradient(0deg,#a9ff00 0%,#007b14 100%);margin-top: -3px;'>日更</span> 新闻早早报</a></h2>";
if($code!='200'){
echo "【微语】<a class='abstract weiyu' style='overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' href='/sixs' title='新闻摘要' rel='noopener noreferrer'></a>";
}else{
echo "<a class='abstract' style='overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' href='/sixs' title='新闻摘要' rel='noopener noreferrer'>" . $a['data']['weiyu'] . "</a>";
}
echo "<div class='item-tags scroll-x no-scrollbar mb6'><a class='but c-blue' title='查看更多分类文章' href='https://www.pengqi.club/'><i class='fa fa-folder-open-o' aria-hidden='true'></i>网络资讯</a><a href='https://www.pengqi.club/sixs' title='查看此标签更多文章' class='but'># 每日60秒早报</a></div><div class='meta'><ul class='items'><li style='display: inline-block;'><a class='avatar-mini' title='pengqi' href='https://www.pengqi.club/author/1'><img alt='pengqi的头像-博客' src='https://cdn.pengqi.club/wp-content/uploads/2021/12/6f36a4098ba8-150x150.png' data-src='https://cdn.pengqi.club/wp-content/uploads/2021/12/6f36a4098ba8-150x150.png' class='avatar avatar- ls-is-cached lazyloaded'><span class='hide-sm ml3'></span></a></li><li class='icon-circle' style='display: inline-block;margin: 0 -6px;'></li><li style='display: inline-block;'>" . date('d', time()) . "日</li><li style='display: inline-block;float: right;'><span class='link'><svg class='icon' aria-hidden='true'><use xmlns:xlink='https://www.w3.org/1999/xlink' xlink:href='#icon-xinwen1'></use></svg> 新闻</span></li></ul></div></div>";
}else{
if($code!='200'){
echo'<script>var str="https://api.03c3.cn/zb/api.php";$.getJSON(str, function(json){var imgid = json.imageUrl;document.getElementById("suolue").src=imgid;});</script><div style="text-align: center;"><img id="suolue" style="width:100%;"></div>';
}else{
echo "<img width='100%' height='100%' class=' lazyloaded' src='" . $a['data']['head_image'] . "' data-src='" . $a['data']['head_image'] . "' alt='新闻早早报'>";
echo "<div style='text-align:center;margin: 20px 0 10px;'><span style='font-size:38px;'>每日新闻</span></div>";
echo "<div style='text-align:center;'><span style='font-size:20px;'>更新时间:".date('Y-m-d', time())."</span></div><br>";
echo "<div><div><h3></h3><span> " . $a['data']['news'][0] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][1] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][2] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][3] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][4] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][5] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][6] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][7] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][8] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][9] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][10] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][11] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][12] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][13] . "</span></div>";
echo "<div><h3></h3><span> " . $a['data']['news'][14] . "</span></div></div>";
echo "<br><div><span style='font-size:16px;margin-left:0;'>" . $a['data']['weiyu'] . "</span></div>";
echo "<div style='text-align:right;margin-top: 20px;'><span style='font-size:20px;'>--- 来自ALAPI</span></div>";
}}};
首页小工具
上面弄好之后就要用到小工具了,在需要的地方添加小工具里的文本写入php代码(外观-小工具-首页主内容上面-文本)
<?php newzaobao()?>
转换成短代码
在新闻60秒早报这段代码最后面添加下面代码:
add_shortcode('zaobao', 'newzaobao');
每日早报页面
在新建页面里添加PHP代码即可:(如果是用后台新建的页面,要在模块里找到自定义HTML代码添加。)
【zaobao】//注意把这个【】换成[],才能显示