0

I have basically worked on this all night and I realize I am quite stuck.

I have a sidebar in my wordpress theme that displays the most recent posts. The sidebar looks like this:

enter image description here

Below each title I want to display the post date in "relative style" e.g "posted two days ago".

I know <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> will produce the relative date but I do not know how to implement this in the same code I already have.

I also need to be able to style this date stamp with custom CSS.

This is the php I have for the sidebar:

<?php
$args = array( 'post_status' => 'publish', 'numberposts' => '30', 'tax_query' => array(
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => 'post-format-aside',
        'operator' => 'NOT IN'
        ), 
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => 'post-format-image',
        'operator' => 'NOT IN'
        )
    ) );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<div class="sidebar-entries">';
    echo get_the_post_thumbnail( $recent['ID'], 'sidebar-thumb', array( 'class' => 'sidebar-image' ) );
    echo '<div class="sidebar-entries-title">';
    echo '<a href="' . get_permalink($recent["ID"]) . '">' .   ( __($recent["post_title"])).'</a></div></div> ';

    // echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
}
?>
0

You don't generate a WP Loop using wp_get_recent_posts() so get_the_time() won't pull the post date. The post date is available within the array wp_get_recent_posts() returns though: $recent['post_date']. Or you can use the modified date: $recent['post_modified'].

This works (tested on my dev site):

<?php
$args = array(
    'post_status' => 'publish', 
    'numberposts' => '30', 
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => 'post-format-aside',
            'operator' => 'NOT IN'
            ), 
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => 'post-format-image',
            'operator' => 'NOT IN'
            )
        ) 
);
$recent_posts = wp_get_recent_posts( $args );
?>
<div class="sidebar-entries">
<?php 
foreach( $recent_posts as $recent ){        
    echo '<div class="sidebar-entry">';
        echo get_the_post_thumbnail( $recent['ID'], 'sidebar-thumb', array( 'class' => 'sidebar-image' ) );
        echo '<div class="sidebar-entries-title">';
            echo '<a href="' . get_permalink( $recent["ID"] ) . '">' . __( $recent["post_title"] ) . '</a>';
            echo '<span class="posted-on">' . human_time_diff( strtotime( $recent['post_date'] ), current_time('timestamp') ) . ' ago </span>';
        echo '</div>';
    echo '</div>';
}
?>
</div>

(A note though, don't just copy/paste code from the Codex. If you don't need the taxonomy argument in there, don't include it.)

| improve this answer | |
  • I am not sure if I completely understand what a taxonomy argument is. I tried to read on it but I didn't quite understand it. This works though. Thanks a lot for the effort! – Arete Apr 17 '16 at 17:48
0

You can do that by by editing the template-tags.php in the inc directory.

Find:

sprintf( '<a class="entry-date" href="%s">%s</a>', esc_url( get_permalink() ), $output_time )

Replace:

sprintf( '<a class="entry-date" href="%s">%s</a>', esc_url( get_permalink() ), $regular_time )

Find:

sprintf( '<a class="entry-date" href="%s">%s</a>', esc_url( get_permalink() ), $output_time ),

Replace:

sprintf( '<a class="entry-date" href="%s">%s</a>', esc_url( get_permalink() ), $regular_time ),
| improve this answer | |
  • I think maybe my question was badly explained. I don't want to replace all time stamps in relative format, only the date in the sidebar. Also I need to know how to implement this in the code I already have so this is not really an answer but thanks for the info. – Arete Apr 17 '16 at 10:44
  • Sorry i am little bit confuse can you explain little more – Owais Alam Apr 17 '16 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.