【问题标题】:get_the_date call not populating within loopget_the_date 调用未在循环中填充
【发布时间】:2019-12-30 07:38:45
【问题描述】:

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

$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>

似乎无法找出问题所在。任何见解将不胜感激

【问题讨论】:

    标签: php html wordpress loops


    【解决方案1】:

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

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

    用这个:

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

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

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2020-09-11
      • 2013-01-19
      相关资源
      最近更新 更多