admin管理员组文章数量:1424923
Thanks for viewing. I am using a soliloquy plugin addon to display post date in slide captions. I want to change the post date to human difference or 'hours ago' format. I'm converting get_the_date
using the following in functions file
// Relative date & time
function wpse_relative_date() {
return human_time_diff( get_the_time('U'), current_time( 'timestamp' ) ) . ' ago';
}
add_filter( 'get_the_date', 'wpse_relative_date' );
and the plugin is
function sol_soliloquy_fc_date_terms( $content, $post, $data ) {
// Start Config
$sliderSlugs = array(
'your-slider-slug-name',
);
// Check slider ID is the one we want to amend
if ( ! in_array( $data['config']['slug'], $sliderSlugs ) ) {
return $content;
}
// Build date and taxonomy terms content
$additional_content =
'<div class="soliloquy-fc-date">'
. __( '', 'soliloquy-featured-content-date-terms' ) . get_the_date( 'dS F Y', $post->ID ) . '
</div>';
// Return content
return $content . $additional_content;
}
add_filter( 'soliloquy_fc_caption', 'sol_soliloquy_fc_date_terms', 1, 3 );
This outputs the amount of days the first post was created and all others are the same so I am guessing it's not returning each posts time because it's outside the loop. I'm a php newbie and I am guessing it's something to do with not being able to use the $post->ID
inside the get_the_date
.
Any help greatly appreciated!
Thanks for viewing. I am using a soliloquy plugin addon to display post date in slide captions. I want to change the post date to human difference or 'hours ago' format. I'm converting get_the_date
using the following in functions file
// Relative date & time
function wpse_relative_date() {
return human_time_diff( get_the_time('U'), current_time( 'timestamp' ) ) . ' ago';
}
add_filter( 'get_the_date', 'wpse_relative_date' );
and the plugin is
function sol_soliloquy_fc_date_terms( $content, $post, $data ) {
// Start Config
$sliderSlugs = array(
'your-slider-slug-name',
);
// Check slider ID is the one we want to amend
if ( ! in_array( $data['config']['slug'], $sliderSlugs ) ) {
return $content;
}
// Build date and taxonomy terms content
$additional_content =
'<div class="soliloquy-fc-date">'
. __( '', 'soliloquy-featured-content-date-terms' ) . get_the_date( 'dS F Y', $post->ID ) . '
</div>';
// Return content
return $content . $additional_content;
}
add_filter( 'soliloquy_fc_caption', 'sol_soliloquy_fc_date_terms', 1, 3 );
This outputs the amount of days the first post was created and all others are the same so I am guessing it's not returning each posts time because it's outside the loop. I'm a php newbie and I am guessing it's something to do with not being able to use the $post->ID
inside the get_the_date
.
Any help greatly appreciated!
Share Improve this question edited Jun 4, 2019 at 9:06 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jun 4, 2019 at 8:46 VisibleVisible 132 bronze badges1 Answer
Reset to default 1In your filter function you should pass post ID to get_the_time()
function, otherwise you will get the date from current post.
function wpse_relative_date( $date, $format, $post ) {
return human_time_diff( get_the_time('U', $post), current_time( 'timestamp' ) ) . ' ago';
}
add_filter( 'get_the_date', 'wpse_relative_date', 15, 3 );
References:
- get_the_time() function
- get_the_date filter
本文标签: plugin developmentDisplay time difference (6 hours ago) in a Soliloquy caption
版权声明:本文标题:plugin development - Display time difference (6 hours ago) in a Soliloquy caption 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745438475a2658335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论