这次为CHATGPT点个赞,为我历史上的今天更新

我的博客很多年前,就装了WP History Post这个插件,实现历史上的今天功能,在文章下可以调出当年今日的所有文章。

本身自己的博客年代久远,所以看起来还很有成就感。

但是调出来的都是今年以前的,无法调出今年的文章,为此横跨两年我在寻求答案。

图片[1]-这次为CHATGPT点个赞,为我历史上的今天更新-臭虾米博客

最后依然没有解决,也这这么凑合着用着。

这两天嫌下来了,于是就又琢磨起来这事了。

我把代码发给CHATGPT,说明我的想法,他就帮我修改了。

当然,一开始修改的有问题,调用出来的是根据今天时间,而非文章的时间,后来才一步一步修改好。

<?php
/*

**************************************************************************

Plugin Name:  WP History Post
Plugin URI:   http://www.arefly.com/wp-history-post/
Description:  Let visitors see your post in Today's History. 讓訪客看到歷史上的今天所寫的文章
Version:      1.0.6
Author:       Arefly
Author URI:   http://www.arefly.com/
Text Domain:  wp-history-post
Domain Path:  /lang/

**************************************************************************

	Copyright 2014  Arefly  (email : eflyjason@gmail.com)

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 2, as 
	published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

**************************************************************************/

define("WP_HISTORY_POST_PLUGIN_URL", plugin_dir_url( __FILE__ ));
define("WP_HISTORY_POST_FULL_DIR", plugin_dir_path( __FILE__ ));
define("WP_HISTORY_POST_TEXT_DOMAIN", "wp-history-post");

/* Plugin Localize */
function wp_history_post_load_plugin_textdomain() {
    load_plugin_textdomain(WP_HISTORY_POST_TEXT_DOMAIN, false, dirname(plugin_basename( __FILE__ )).'/lang/');
}
add_action('plugins_loaded', 'wp_history_post_load_plugin_textdomain');

include_once WP_HISTORY_POST_FULL_DIR."options.php";

/* Add Links to Plugins Management Page */
function wp_history_post_action_links($links){
    $links[] = '<a href="'.get_admin_url(null, 'options-general.php?page='.WP_HISTORY_POST_TEXT_DOMAIN.'-options').'">'.__("Settings", WP_HISTORY_POST_TEXT_DOMAIN).'</a>';
    return $links;
}
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'wp_history_post_action_links');

function wp_history_post_base($post_month, $post_day) {
    global $wpdb;
    $limit = 30; // 限制显示文章数量
    $order = "DESC"; // 按最新排序

    $sql = "SELECT ID, year(post_date) AS h_year, post_title, comment_count 
            FROM $wpdb->posts 
            WHERE post_password = '' 
            AND post_type = 'post' 
            AND post_status = 'publish'
            AND month(post_date) = %d 
            AND day(post_date) = %d
            ORDER BY post_date $order 
            LIMIT %d";

    $prepared_sql = $wpdb->prepare($sql, $post_month, $post_day, $limit);
    $history_post = $wpdb->get_results($prepared_sql);

    return $history_post;
}

/*********************** WP HISTORY POST SINGLE POST PART START ***********************/

function wp_history_post_single() {
    global $post;
    if (!$post) {
        return ''; // 如果当前不是单篇文章,返回空
    }

    // 获取当前文章的日期
    $post_month = get_the_date('m', $post->ID);
    $post_day = get_the_date('d', $post->ID);

    $h_post = '';
    $histtory_post = wp_history_post_base($post_month, $post_day);
    if ($histtory_post) {
        foreach ($histtory_post as $post_item) {
            $h_year = $post_item->h_year;
            $h_post_title = $post_item->post_title;
            $h_permalink = get_permalink($post_item->ID);

            // 添加文章页的特定样式类
            $h_post .= '<p class="history-single-item"><span class="history-year">' . $h_year . ':</span> <a href="' . $h_permalink . '" title="' . $h_post_title . '" rel="external nofollow">' . $h_post_title . '</a></p>';
        }
    }
    if ($h_post) {
        $result = '<h2 class="history-title">' . get_option("wp_history_post_content_title") . '</h2>' . $h_post;
    }
    return $result ?? ''; // 确保返回值存在
}

function wp_history_post_content($content) {
    if (is_single() && wp_is_mobile()) { // 仅在移动端加载
        $content .= wp_history_post_single(); // 加载文章页内容
    }
    return $content;
}
add_action('the_content', 'wp_history_post_content');

/*********************** WP HISTORY POST WIDGET PART START ***********************/

function wp_history_post_widget() {
    global $post;
    if (!$post) {
        return ''; // 如果当前不是单篇文章,返回空
    }

    // 获取当前文章的日期
    $post_month = get_the_date('m', $post->ID);
    $post_day = get_the_date('d', $post->ID);

    $h_post = '';
    $histtory_post = wp_history_post_base($post_month, $post_day);
    if ($histtory_post) {
        foreach ($histtory_post as $post_item) {
            $h_year = $post_item->h_year;
            $h_post_title = $post_item->post_title;
            $h_permalink = get_permalink($post_item->ID);

            // 添加侧边栏的特定样式类
            $h_post .= '<p class="history-widget-item"><span class="history-year">' . $h_year . ':</span> <a href="' . $h_permalink . '" title="' . $h_post_title . '" rel="external nofollow">' . $h_post_title . '</a></p>';
        }
    }
    return $h_post;
}

class wp_history_post_widget extends WP_Widget {
    function __construct() {
        $widget_ops = array(
            'classname' => 'wp_history_post_class',
            'description' => __("See your Today's History Post.", WP_HISTORY_POST_TEXT_DOMAIN)
        );
        parent::__construct(false, $name = __('WP History Post', WP_HISTORY_POST_TEXT_DOMAIN), $widget_ops);
    }

    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
        $no_post_notice = $instance['no_post_notice'];
        echo $before_widget;
        echo '<div class="' . WP_HISTORY_POST_TEXT_DOMAIN . '">';
        if (empty($title)) {
            $title = "Today's History Post";
        }
        echo $before_title . '<h2 class="history-widget-title">' . $title . '</h2>' . $after_title;

        $history_post = wp_history_post_widget();
        if ($history_post) {
            echo $history_post;
        } else {
            if ($no_post_notice) {
                echo $no_post_notice;
            } else {
                _e("There are no History Post Today!", WP_HISTORY_POST_TEXT_DOMAIN);
            }
        }
        echo '</div>';
        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['no_post_notice'] = strip_tags($new_instance['no_post_notice']);
        return $instance;
    }

    function form($instance) {
        $title = $instance['title'] ?? '';
        $no_post_notice = $instance['no_post_notice'] ?? '';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', WP_HISTORY_POST_TEXT_DOMAIN); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('no_post_notice'); ?>"><?php _e("No History Post's Notice", WP_HISTORY_POST_TEXT_DOMAIN); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('no_post_notice'); ?>" name="<?php echo $this->get_field_name('no_post_notice'); ?>" type="text" value="<?php echo $no_post_notice; ?>" />
        </p>
        <?php
    }
}

function wp_history_post_register_widgets() {
    register_widget('wp_history_post_widget');
}
add_action('widgets_init', 'wp_history_post_register_widgets');

/*********************** Custom Styles for Widget and Single Post ***********************/

function wp_history_post_custom_styles() {
    $custom_css = "
        /* 通用样式 */
        .history-title, .history-widget-title {
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
            border-bottom: 2px solid #0073aa; /* 添加竖线 */
            padding-bottom: 5px; /* 与标题内容保持间距 */
        }

        .history-title {
            font-size: 16px; /* 文章页标题字体 */
        }
        .history-widget-title {
            font-size: 16px; /* 侧边栏标题字体 */
        }

        .history-year {
            color: blue; /* 年份字体为蓝色 */
            font-weight: bold;
        }
        .history-year + a {
            color: #0073aa;
            text-decoration: none;
        }
        .history-year + a:hover {
            text-decoration: underline;
        }

        /* 文章页样式 */
        .history-single-item {
            margin: 5px 0; /* 控制文章页的间距 */
            line-height: 1.4; /* 行间距 */
            font-size: 14px;
        }

        /* 侧边栏样式 */
        .history-widget-item {
            margin: 3px 0; /* 控制侧边栏的紧凑间距 */
            font-size: 13px;
            line-height: 1.4;
        }
    ";
    wp_add_inline_style('wp-block-library', $custom_css);
}
add_action('wp_enqueue_scripts', 'wp_history_post_custom_styles');

修改这个文件:wp-history-post.php

这里我修改了几处:

1、修改调出所有今日的文章

2、在电脑端只显示侧边栏,在手机端历史上的今天显示在文章页下。

图片[2]-这次为CHATGPT点个赞,为我历史上的今天更新-臭虾米博客

感觉还不是很完美,不过能解决我的问题,这才是最关键的,这点很值得赞下CHATGPT

我在22年时就写过关于历史上的今天的一些记录,后面有时间了把2者结合起来,自己搞一个小插件,适合自己的。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容