Same date is repeating on my custom 'Most Recent Posts' on sidebar

admin2025-06-02  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!

/

<!-- [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>

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/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>
Share Improve this question edited Mar 15, 2019 at 19:01 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 15, 2019 at 18:26 Jimmy SollerJimmy Soller 111 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 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>

Replace

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

with

  <span>  <?php echo get_the_date('F j, Y', $value->ID); ?> </span>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748802281a313841.html

最新回复(0)