Site logo

Archived topic

Modifying Post Date Blocks?

7 replies · Started by Nicholas on February 16, 2022

Viewing posts 1–8 of 8

Is there a way to add new date formats to the "Post Date" block? I'm using a post template to show some of the most recent posts and I'd like to only display the full month and day.

Unfortunately neither solution will work for what I'm trying to achieve. I'm displaying a few recent posts on a front page using the "latest posts" block, which includes a "query loop" and "post template" block.

The format setting for the "post date" block does not have an option for the month and day. I specifically want this date format for the recent posts only. I still want the year to show everywhere else so I cannot change the site settings.

I have also tried a couple different date hooks which are not working because they are pulling the date of the page instead of the posts from the query loop.

Yes, you’re correct. Looking at the WordPress repository for Latest Posts block, I cant find a hook we can use.

See: https://github.com/WordPress/WordPress/blob/ad48a153873cdef28ab03c54a034de807e729052/wp-includes/blocks/latest-posts.php

Maybe you can use a plugin like WP Show Posts which is soon to be merged with GenerateBlocks Pro?

Through this, we can hook manually a uniquely formatted date or figure out another way possible.

Here is a sample: https://share.getcloudapp.com/llu6pgYE

As shown, I hooked the date to wpsp_after_title, a hook available in WPSP.

Here is how it looks in my test site: https://share.getcloudapp.com/L1uWZ6N6

Hope this helps! :)

Unfortunately I have no other option than to write my own function to recreate the entire recent post query, or to manually edit the block functionality (which won't stick with theme/GB updates) all because the date cannot be edited further.

The date format options for the 'post date' block should really have all possible and acceptable strings that the wp the_date function accepts. Frankly this appears to be a lazy oversight and one that can hopefully be updated in the near future.

You can filter the dynamic post date Headline from GB and here is a helpful thread you may refer to regarding this: https://generatepress.com/forums/topic/modifying-post-date-blocks/#post-2123025

As for the Latest Posts block, this is a WP core block and is out of our scope of support.

Hope you work things out! Feel free to reach out anytime if you’ll need assistance with anything else. :)

I appreciate you following up and providing a reference link, but that's the URL of my previous forum post.

Sorry about that. Here is the correct link:

https://generatepress.com/forums/topic/change-published-date-format/#post-1804072

Specifically, here is a code which might suit you:

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    $text_type = $block['attrs']['gpDynamicTextType'];
    if ( ! empty( $text_type ) && 'post-date' === $text_type && '5c88c8e5' === $block['attrs']['uniqueId'] ) {
        $text = sprintf(
		'<time class="entry-date published" datetime="%1$s">%2$s</time>',
		esc_attr( get_the_date( 'c', $id ) ),
		esc_html( get_the_date( 'F j', $id ) )
	);
    }
    return $text;
}, 10, 2 );

Kindly replace the 5c88c8e5 with the unique ID of the Headline block for the post date.

See: https://share.getcloudapp.com/YEuB2R85

Also see: https://share.getcloudapp.com/xQuz2bZ1

Hope this helps! :)

This archived topic is closed to new replies.