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
1 Answer
Reset to default 1get_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.
- The link (
<a href=%3$s
) has a placeholder for variable #3, a string. - 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. - 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
评论列表(0条)