$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Same date is repeating on my custom 'Most Recent Posts' on sidebar|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far