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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

In 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