You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress 6.7.2:未发布文章标题显纯文本,发布后转链接的实现方法

WordPress 6.7.2 实现排期文章标题自动切换文本/链接的小工具

实现步骤

1. 添加自定义小工具代码

把下面的代码加到主题的functions.php文件里,或者用自定义代码插件来添加:

class Scheduled_Posts_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'scheduled_posts_widget',
            '排期/已发布文章列表',
            array('description' => '显示排期文章标题,发布后自动转为链接')
        );
    }

    public function widget($args, $instance) {
        echo $args['before_widget'];
        if (!empty($instance['title'])) {
            echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
        }

        // 查询排期和已发布文章,按发布时间排序
        $posts_query = new WP_Query(array(
            'post_status' => array('future', 'publish'),
            'posts_per_page' => !empty($instance['num_posts']) ? $instance['num_posts'] : 5,
            'orderby' => 'date',
            'order' => 'ASC'
        ));

        if ($posts_query->have_posts()) {
            echo '<ul>';
            while ($posts_query->have_posts()) {
                $posts_query->the_post();
                $post_title = get_the_title();
                
                // 根据文章状态输出内容
                if (get_post_status() === 'publish') {
                    $post_link = get_permalink();
                    echo '<li><a href="' . esc_url($post_link) . '">' . esc_html($post_title) . '</a></li>';
                } else {
                    echo '<li>' . esc_html($post_title) . '</li>';
                }
            }
            echo '</ul>';
            wp_reset_postdata();
        } else {
            echo '<p>暂无排期或已发布文章</p>';
        }

        echo $args['after_widget'];
    }

    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : '即将发布/已发布文章';
        $num_posts = !empty($instance['num_posts']) ? $instance['num_posts'] : 5;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">标题:</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 esc_attr($title); ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('num_posts'); ?>">显示文章数量:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('num_posts'); ?>" name="<?php echo $this->get_field_name('num_posts'); ?>" type="number" value="<?php echo esc_attr($num_posts); ?>" min="1">
        </p>
        <?php
    }

    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['num_posts'] = (!empty($new_instance['num_posts'])) ? intval($new_instance['num_posts']) : 5;
        return $instance;
    }
}

// 注册小工具
function register_scheduled_posts_widget() {
    register_widget('Scheduled_Posts_Widget');
}
add_action('widgets_init', 'register_scheduled_posts_widget');

2. 配置小工具

  • 进入WordPress后台的外观 > 小工具页面
  • 找到「排期/已发布文章列表」小工具,添加到你需要的侧边栏或小工具区域
  • 设置标题和要显示的文章数量,保存即可

核心逻辑说明

  • 用WP_Query同时抓取future(排期)和publish(已发布)状态的文章
  • 通过get_post_status()判断文章状态:
    • 已发布的文章,输出带永久链接的超链接标题
    • 未发布的排期文章,直接输出纯文本标题
  • 所有输出都做了安全转义,避免恶意代码注入

内容的提问来源于stack exchange,提问作者Keith Davies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:18:29