个性化阅读
专注于IT技术分析

获取WordPress中帖子日期的问题

点击下载

这是我的index.php代码

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <?php 
        if (have_posts()) {
            while (have_posts()) {
                the_post(); 
        ?>
        <div class="col-sm-6">
            <div class="main-post">
                <a href="<?php the_permalink(); ?>"><h3 class="post-title"><?php the_title(); ?></h3></a>
                <a href="<?php get_the_author_link() ?>"><span class="author"><i class="ion-android-person"></i><?php the_author(); ?></span></a>
                <span class="date"><i class="ion-ios-clock"></i><?php the_date('F j, Y'); ?></span>
                <span class="comments"><i class="ion-chatbubble-working"></i>20 comments</span>
                <img class="img-thumbnail img-fluid" src="https://placehold.it/600x200/555" alt="">
                <p class="post-content">Mouth. It when to finds at should, for queen more uninitiated to what are negotiations the reached understood. Making to company, it's musical for notice and with are he my size all having bad completely ran explain right the counter-productive like in just energetic other misleads brains the to more.</p>
                <hr>
                <p class="categories"><i class="ion-pricetag"></i><?php the_category(', ') ?></p>
            </div>
        </div>
        <?php
            } // end while
        } // end if
        ?>
    </div><!-- end row-->
</div><!--end container-->

<?php get_footer(); ?>

这是我在浏览器中得到的:

浏览器中的帖子

我的问题是:为什么日期仅出现在第一篇文章中?


#1


尝试这个:

<span class="date">
    <i class="ion-ios-clock"></i><?php echo get_the_date('F j, Y'); ?>
</span>

代替这个:

<span class="date">
    <i class="ion-ios-clock"></i><?php the_date('F j, Y'); ?>
</span>

解释

以下解释不是我自己的;这是由凯撒(Kaiser)-WPDSE, 所以。

当你查看the_date()函数的源代码时, 你会注意到两个全局变量:

global $currentday, $previousday;

然后有一个规则, 即是否要显示日期…。该检查类似于使用is_new_day()进行的检查:

if ( $currentday != $previousday ) {

    // show date

    // Set global
    $previousday = $currentday;
}
// else
return null;

如你所见, $ previousday立即设置为$ currentday;。因此, 它会回声一次。在那之后, 这两天都是一样的, 检查将失败。这就是为什么你的第一篇文章显示它, 而其他人则不显示它的原因。


参考:如果其他帖子具有相同的日期, 则该帖子不显示日期

赞(0)
未经允许不得转载:srcmini » 获取WordPress中帖子日期的问题

评论 抢沙发

评论前必须登录!