WordPress显示即将发布的文章列表

如果为新文章设置了定时发布,是否想在显著的位置放个提示,告诉读者马上将会有什么文章发布,可能会帮助你的博客获得更多的关注,这里有两种方法可以实现上述功能。

WordPress显示即将发布的文章列表

方法一:将下面代码添加到主题模板适当的位置即可。

<ul>
<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=10&ignore_sticky_posts=1');
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post();
        $do_not_duplicate = $post->ID; ?>
        <li><?php the_time('H:i') ?> <?php the_title(); ?></li>
    <?php endwhile;
}
?>
</ul>

方法二、将下面的代码添加到当前主题functions.php文件中:

function future_posts_function($atts){
    extract(shortcode_atts(array(
        'poststatus' => 'future',
        'order'         => 'DESC',
        'showposts' => 10,
        'ignore_sticky_posts' => 1
    ), $atts));
    $return_string = '<ul>';
    query_posts(array('post_status' => $poststatus, 'order' => $order, 'ignore_sticky_posts' => $ignore_sticky_posts, 'showposts' => $showposts));
    if (have_posts()) :
        while (have_posts()) : the_post();
            $return_string .= '<li>'.get_the_title().'</li>';
        endwhile;
    endif;
    $return_string .= '</ul>';
    wp_reset_query();
    return $return_string;
}
add_shortcode('future_posts', 'future_posts_function');
// 让文本小工具支持短代码
add_filter('widget_text', 'do_shortcode');

之后在文本小工具中添加短代码:

[future_posts]

主题用户添加上述代码就更简单了,直接将方法一的代码放到增强文本小工具中即可,无需修改主题,效果如下图:

WordPress显示即将发布的文章列表
最终效果
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索