0

I am having an impossible time finding the proper way to add a date below the recent posts widget links. If anybody has a code, I would greatly appreciate it!

4

This is a default feature which by default is set not to show the post date. Here is the part of the code responsible to show/hide the post date

<?php if ( $show_date ) : ?>
    <span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>

To enable the post date, just check the Display post date? checkbox

enter image description here

EDIT

Here is an idea how to change the post date format (or for that matter changing the post date to anything) for only the recent post widget

Here we will run a filter function on the get_the_date filter inside the widget_display_callback filter. We will use the parameters passed to the widget_display_callback filter to target only the recent post widget only when the show_date instance is set to true

add_filter( 'widget_display_callback', function ( $instance, $widget_instance )
{
    if (    $widget_instance->id_base === 'recent-posts'
         && $instance['show_date']    === true
    ) {

        add_filter( 'get_the_date', function ( $the_date, $d, $post )
        {
            // Set new date format
            $d = 'Y-m-d';
            // Set new value format to $the_date
            $the_date = mysql2date( $d, $post->post_date );

            return $the_date;   
        }, 10, 3 );

    }
    return $instance;
}, 10, 2 );

You can use the idea above to target the post title and permalink of the post via the the_title and the post_link filters respectively in the recent posts widget. The above idea is also not just restricted to the recent post widget.

If you need anymore control over the output, I would suggest that you copy the recent posts widget to your custom plugin and then modifying it as necessary

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.