Home > Back-end >  PHP function only returns date on some posts
PHP function only returns date on some posts

Time:03-08

I am using the following code in my wordpress page:

<article>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<section>
<h1><?php the_title(); ?></h1>
<p>Uppladdat <?php echo the_date('I M Y');?> <?php echo get_post_time('H:i:s'); ?></p>
<p><?php the_content(); ?></p>
</section>
<?php endwhile; endif;?>
</article>

And on the first blog post, it shows the date and time it's posted. On the second post, it shows only the time. On the third one, it shows date and time again.

What is happening here?

CodePudding user response:

the_date() only gives an output if it's different from the previous post. It is also echoed automatically, so no need for "echo the_date()"

use echo get_the_date() instead - get_the_date() will always return a value.

https://developer.wordpress.org/reference/functions/the_date/ https://developer.wordpress.org/reference/functions/get_the_date/

  • Related