首页 > 解决方案 > get_the_date 调用未在循环中填充

问题描述

我的新闻模板中有一个最近的帖子循环,由于某种原因,它正在拉除日期之外的所有内容。

$recent_posts = wp_get_recent_posts(array(
            'numberposts' => 15,
            'post_status' => 'publish'
        ));
        ?>
        <div class="row">
            <?php foreach($recent_posts as $post) : ?>
                <div class="news-item-block col-md-4" role="article">
                    <a class="news-item-image-link" href="<?php echo get_permalink($post['ID']) ?>">
                        <?php echo get_the_post_thumbnail($post['ID'], 'news-grid-image'); ?>
                    </a>
                    <span class="news-item-date"><?php echo get_the_date( 'M d, Y' ); ?></span>
                    <a class="news-item-title" href="<?php echo get_permalink($post['ID']) ?>">
                        <h1><?php echo $post['post_title'] ?></h1>
                    </a>
                </div>
            <?php endforeach; ?> 
        </div>

似乎无法弄清楚问题所在。任何见解将不胜感激

标签: phphtmlwordpressloops

解决方案


当您使用 foreach 循环时,您需要传递帖子 ID 以获取该帖子的日期。替换以下行:

<span class="news-item-date"><?php echo get_the_date( 'M d, Y' ); ?></span>

有了这个:

<span class="news-item-date"><?php echo get_the_date('M d, Y', $post['ID']); ?></span>

你会得到你的帖子的完美日期


推荐阅读