Show post updated date instead of published

I need simple solution to show updated date instead of published without adding any functions. I have read few articles,

I need simple solution to show updated date instead of published without adding any functions. I have read few articles, but they are bit complex using "dirty" code. This is my current code is it possible to modify this code to show updated date:

    $date = sprintf( '<span>' . __( 'Posted', 'theme' ) . ' <a href="%3$s"><time class="entry-date" datetime="%1$s">%2$s</time></a></span>',
    esc_attr( get_the_date( 'c' ) ),
    esc_html( time_ago() ),
    esc_html( get_day_link( get_the_date('Y'), get_the_date('m'), get_the_date('d') ) )
  );

I have tried with get_the_modified_date, but this did not produce updated date:

/

Thanks!

I need simple solution to show updated date instead of published without adding any functions. I have read few articles, but they are bit complex using "dirty" code. This is my current code is it possible to modify this code to show updated date:

    $date = sprintf( '<span>' . __( 'Posted', 'theme' ) . ' <a href="%3$s"><time class="entry-date" datetime="%1$s">%2$s</time></a></span>',
    esc_attr( get_the_date( 'c' ) ),
    esc_html( time_ago() ),
    esc_html( get_day_link( get_the_date('Y'), get_the_date('m'), get_the_date('d') ) )
  );

I have tried with get_the_modified_date, but this did not produce updated date:

https://developer.wordpress/reference/functions/get_the_modified_date/

Thanks!

Share Improve this question asked Mar 27, 2019 at 21:01 Benjamin FranklinBenjamin Franklin 532 silver badges9 bronze badges 5
  • 1 And what have it produced? Could you show your code with get_the_modified_date? – Krzysiek Dróżdż Commented Mar 27, 2019 at 21:05
  • I have just replaced get_the_date with get_the_modified_date. – Benjamin Franklin Commented Mar 27, 2019 at 21:28
  • Could you show that code exactly? It's hard to say what is wrong with code that you can't see ;) – Krzysiek Dróżdż Commented Mar 27, 2019 at 21:29
  • Even with my limited PHP knowledge I can see that this updated code can't work. No point of sharing it. – Benjamin Franklin Commented Mar 27, 2019 at 21:37
  • As explained in comment above: I have just replaced get_the_date with get_the_modified_date – Benjamin Franklin Commented Mar 27, 2019 at 21:54
Add a comment  | 

1 Answer 1

Reset to default 1

get_the_modified_date should do what you're looking for. I am guessing that perhaps you are struggling a bit with the sprintf syntax.

 $date = sprintf( '<span>' . __( 'Posted', 'theme' ) . ' <a href="%3$s"><time class="entry-date" datetime="%1$s">%2$s</time></a></span>',
    esc_attr( get_the_date( 'c' ) ),
    esc_html( time_ago() ),
    esc_html( get_day_link( get_the_date('Y'), get_the_date('m'), get_the_date('d') ) )
  );

Let's break it down:

sprintf argument 1 - the string with placeholders

sprintf( '<span>' . __( 'Posted', 'theme' ) . ' <a href="%3$s"><time class="entry-date" datetime="%1$s">%2$s</time></a></span>',

This is the main string that will be built. It has 3 placeholders. However, they are in a non-traditional order.

  1. The link (<a href=%3$s) has a placeholder for variable #3, a string.
  2. The datetime attribute (datetime=%1$s), which is part of the HTML time element that makes the time machine-readable (useful for advanced features, and if the time element contains more than just the actual time), has a placeholder for variable #1, a string.
  3. And finally the displayed time (>%2$s</time>) is looking for variable #2, a string.

sprintf arguments 2,3, and 4 - the values

Those 3 variables are provided in the following function arguments (again, not in the order they are used):

esc_attr( get_the_date( 'c' ) ),
esc_html( time_ago() ),
esc_html( get_day_link( get_the_date('Y'), get_the_date('m'), get_the_date('d') ) )

The first, which is feeding into that datetime= attribute, is getting the post's published date and outputting it in the 'c' format, which is a standard date format that looks like 20019-03-27T15:19:21+00:00.

The second, which is the displayed time, is referencing a function I don't recognized, called time_ago().

The third, which is being used for the link href=, is generating the link based off of the post's published date.

So, to properly change the date and still use that function, we should adjust all 3 parameters.

$date = sprintf( '<span>' . __( 'Posted', 'theme' ) . ' <a href="%3$s"><time class="entry-date" datetime="%1$s">%2$s</time></a></span>',
    esc_attr( get_the_modified_date( 'c' ) ),
    esc_html( get_the_modified_date(),
    esc_url( get_day_link( get_the_modified_date('Y'), get_the_modified_date('m'), get_the_modified_date('d') ) )
  );

Note I changed the 3rd parameter to esc_url since it is a URL that is being generated, and that the display of the output date can be adjusted by passing in PHP date formatting options, such as get_the_modified_date( 'm-d-Y' )

Finally, all of this is getting stored in your $date variable as HTML, so make sure to actually echo it to the page before you're done.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745652025a4638322.html

相关推荐

  • Show post updated date instead of published

    I need simple solution to show updated date instead of published without adding any functions. I have read few articles,

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信