1

If you look at the 'most recent posts' on my sidebar for my posts page, you can see that the 'date' is repeating. The code and page is below. Any help would be appreciated. Thanks!

https://cenbrandlab.org/home-blog/

<!-- [SINGLEPOST SIDEBAR MOST POPULAR ARTICLES] [START]-->
<div class="c-singlepost__sidebar__articles">
    <h3 class="c-singlepost__sidebar__articles-title"><?php the_field("side_post_list_title", "option"); ?></h3>

    <?php

    $args = [
            "numberposts" => 5
    ];
     $recent_posts = get_posts($args);

//         echo "<pre>";
//         echo var_dump($recent_posts);
//         echo "</pre>";

     foreach ($recent_posts as $value):

    ?>

     <a href="<?php echo get_permalink($value->ID); ?>">
        <div class="c-singlepost__sidebar__articles-item">
            <div class="c-singlepost__sidebar__articles-item-image">
                <img src="<?php echo get_field("thumbnail_image",$value->ID); ?>"  class="responsive-image"/>

            </div>
            <div class="c-singlepost__sidebar__articles-item-right">
                <div class="c-singlepost__sidebar__articles-item-date">
                    <span>  <?php echo get_the_date('F j, Y'); ?> </span>
                </div>
                <a href="<?php echo get_permalink($value->ID); ?>" class="c-singlepost__sidebar__articles-item-post">
                    <?php echo $value->post_title ?>
                </a>

            </div>
        </div>
     </a>
1

The same date is always displayed because of the use of echo get_the_date('F j, Y'). As you can read in the documentation:

The get_the_date template tag retrieves the date the current $post was written.

Change get_the_date('F j, Y') to get_the_date('F j, Y', $value->ID) and it should work.

Or without additional DB queries:

<div class="c-singlepost__sidebar__articles-item-date">
    <span>  <?php 
         $date = \DateTime::createFromFormat('Y-m-d H:i:s', $value->post_date);
         echo ($date !== FALSE) ? $date->format('F j, Y') : ''; 
    ?> </span>
</div>
0

Replace

  <span>  <?php echo get_the_date('F j, Y'); ?> </span>

with

  <span>  <?php echo get_the_date('F j, Y', $value->ID); ?> </span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.