7b2主题快讯版每日60秒新闻

7b2主题快讯版每日60秒新闻

一、内容接口是调用的知乎,https://www.zhihu.com,此代码仅用于7b2主题,其它主题可修改文件中的sql语句即可。图片是保存到您的媒体库里面的。

1、网站的根目录新新建一个php文件,将代码复制到此文件里面。

2、访问您的域名+/这个文件的名称。

3、完成。

二、文件代码:

<?php

require_once 'wp-config.php';
require_once('wp-admin/includes/media.php');
require_once('wp-admin/includes/file.php');
require_once('wp-admin/includes/image.php');

class XT_NEWS{
    
    private const API_URL = "https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items";
    
    private $data;
    
	public function __construct(){
	    
        global $wpdb;
	    
        date_default_timezone_set('PRC');

        $this->data = json_decode(file_get_contents(self::API_URL));

        $data = $this->data;
        
        $title = $data->data[0]->title;

        $posts = $wpdb->get_row("SELECT id from $wpdb->posts where post_title = '$title' ");
        
        if (!$posts) {
            
            self::XT_Release();

        }else{
            
            echo '今日已发布-'.$title;
        }

	}
    
    private function XT_Release(){
        
        global $wpdb;
        
        $date = $this->data;
        
        $content = $date->data[0]->content;
        
        $content = preg_replace('/(<a.*?>[\s\S]*?<\/a>)/', '', $content);
        
        $pattern = '<img.*?src="(.*?)">';
        
        $rand_name = self::XT_thumbnail($pattern, $content);
        
        $content = strip_tags($content, '<p>');
        $content = '<img class="size-full wp-image-156 aligncenter" src="' . $rand_name . '" alt="" width="720" height="350" />' . $content;
        
        $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 = 'newsflashes_tags' ")->term_taxonomy_id;
        if (!$term_taxonomy_id) {
            $wpdb->query("insert into $wpdb->terms (name,slug,term_group)VALUES('60秒读懂世界','60s','0')");
            $category_id = $wpdb->insert_id;
            $wpdb->query("insert into $wpdb->term_taxonomy (term_id,taxonomy,description,parent,count)VALUES($category_id,'newsflashes_tags','','0','1')");
            $term_taxonomy_id = $wpdb->insert_id;
        }
        $post_tag_arr[] = $term_taxonomy_id;
        
        $html = $content;
        
        //标题
        $title = $date->data[0]->title;

            $now = current_time('mysql');
            $now_gmt = current_time('mysql', 1);
            $wpdb->insert(
                $wpdb->posts,
                array(
                    'post_author' => 37,
                    '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, ","));

    }
    
    
    
    private function XT_thumbnail($pattern, $content){
        
        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("uploads/")) {
            
            mkdir("uploads/", 0777, true);
        }
        
        imagejpeg($new_image, "uploads/" . $rand_name);
        
        imagedestroy($src);
        
        imagedestroy($new_image);
        
        $url =  __DIR__ . '/uploads/'.$rand_name;

        return self::xt_get_attachment_url($url,$rand_name);

    }
    
    private function xt_get_attachment_url($url,$rand_name){

            $file_array = [
                'name' => basename($url),
                'tmp_name' => $url
            ];
            
            
            $attach_id = media_handle_sideload($file_array);

            return wp_get_attachment_url($attach_id);
    }
    
}
new XT_NEWS();

以上代码如有问题,请在下方下载该代码。

下载权限
查看
  • 免费下载
    评论并刷新后下载
    登录后下载
  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余
一、本站上的部份代码及教程来源于互联网,仅供网友学习交流。如有侵权,无意侵害您的权益,请发送邮件至zhangshen#ahap.cn或点击右侧 联系我们,我们将尽快处理。
二、请勿将购买的资源教程转载或分享与他人!

给TA充电
共{{data.count}}人
人已充电
默认

个人主页HTML源码

2022-7-27 13:13:49

默认

WordPress网站地图

2022-8-7 2:20:47

32 条回复 A文章作者 M管理员
  1. 飞机头

    感谢分享

  2. 爱腾讯,爱生活

    冲冲冲

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索