【问题标题】:Get current month posts in Wordpress loop在 Wordpress 循环中获取当前月份的帖子
【发布时间】:2015-06-10 13:16:11
【问题描述】:

我有我的循环,不幸的是,它只捕获当前月份之前的所有帖子。所以我最终得到了当月(2015 年 4 月)没有显示的帖子。

如何让循环也包含当前月份的帖子?
我的代码:

<?php function the_archive () {
    global $wpdb;
    $limit = 0;
    $year_prev = null;
    $months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");

    foreach($months as $month) :
        $year_current = $month->year;

        if ($year_current != $year_prev) {
            if ($year_prev != null) {} ?>

            <li class="archive-year">
                <a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/">
                    <?php echo $month->year; ?>
                </a>
            </li>

        <?php } ?>

        <li class="archive-month">
            <a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>/">
                <?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>
            </a>
        </li>

        <?php $year_prev = $year_current;
        if(++$limit >= 18) { break; }

    endforeach; 
}
add_shortcode( 'show_archive' , 'the_archive' );
?>

上面输出了一个导航存档帖子的菜单 - 请参阅:http://imgur.com/C4WIiKR & http://codeandco.net/news/

所以我希望看到 APRIL 的菜单项,但我没有。
有什么想法吗?
(提前感谢 :-)

【问题讨论】:

    标签: php wordpress for-loop foreach


    【解决方案1】:

    所以我想通了。该代码实际上运行良好,但可能由于时区(我猜)存在一个小问题。

    当我在澳大利亚时,美国(也许 Wordpress 的核心日期可能是从那里计算的)在时间上落后了。所以我在 2015 年 4 月 6 日发布了我的帖子 - 但它没有出现。

    当我将帖子上的发布日期更改为前一天(2015 年 4 月 5 日)时,它按预期工作,没有任何问题!

    这归结为一个简单的事实,即在 Wordpress 中:您不能发布过时的帖子。它们只能被“安排”在未来的某个日期发布。

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 2011-06-21
      • 1970-01-01
      • 2017-04-30
      • 2019-06-23
      • 1970-01-01
      • 2019-05-28
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多