Archived topic
Filter post date within the new query loop block
3 replies · Started by Kyle on July 19, 2022
I'm trying to filter the post date within the query loop block.
add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
error_log( print_r( 'stuff', 1) );
$text_type = $block['attrs']['gpDynamicTextType'];
if ( ! empty( $text_type ) && 'post-date' === $text_type ) {
$text = sprintf(
'<time class="entry-date published" datetime="%1$s"><span>%2$s</span><span>%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F', $id ) ),
esc_html( get_the_date( 'J', $id ) )
);
}
return $text;
}, 10, 2 );
See screenshot as well https://www.evernote.com/l/AOEPogXsly9PMKDpcm2GBrIExsZc2t3EnSk
Currently I can't even get the filter to work. I've tried outputting to the debug log with no success.
Could someone help?
Hi there,
that filter only applies to the GP Dynamic data stuff in the Block Element.
In the Block Editor you would need to use the core render_block filter - something like this:
add_filter( 'render_block', function( $block_content, $block ){
if ( ! empty( $block['attrs']['className'] ) && 'my-class' === $block['attrs']['className'] ) {
$block_content = sprintf(
'<time class="entry-date published" datetime="%1$s"><span>%2$s</span><span>%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F', $id ) ),
esc_html( get_the_date( 'J', $id ) )
);
}
return $block_content;
}
} );
Note: you need to give the block an Advanced > Additional CSS Class and replace the my-class with that in the code
Thanks,
Final code for anyone who finds this page:
add_filter( 'render_block', function( $block_content, $block ) {
if ( ! empty( $block['attrs']['className'] ) && 'news-date' === $block['attrs']['className'] ) {
$block_content = sprintf(
'<time class="entry-date published" datetime="%1$s"><span>%2$s</span><span>%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F', $id ) ),
esc_html( get_the_date( 'j', $id ) )
);
}
return $block_content;
}, 10, 2 );
You're welcome !