I am using a custom HTML block to create a customized title cover for a post on my WordPress site. I would like to get the current post publication date (rather than hard-coding it in and having to update manually). Is there a CSS selector or JavaScript function or I can use from within a custom HTML block to get the publication date?

  • I can't think of a particularly elegant way to accomplish this. You could certainly use JS to grab it from somewhere else in the markup - but that won't preview correctly in the Block Editor without further work. Or you could always grab it from the REST API, but that seems fairly excessive. I think ideally you'd compose your title cover from Blocks instead of straight HTML, in which case a block or custom shortcode could provide the date for you.
    – bosco
    Aug 10 at 20:28
  • WordPress core has a post date block. You could either use that or refer to its source for guidance: github.com/WordPress/gutenberg/blob/trunk/packages/… That block uses useEntityProp() to get the date. Aug 11 at 3:00
  • Now that I'm not on my phone I can see I misunderstood and thought you were creating a block, not using a custom HTML block. If you can create a block I'd advise that, otherwise you'll probably just need to use an old fashioned shortcode. Aug 11 at 6:53


Your Answer

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

Browse other questions tagged or ask your own question.