Archived topic
Post format is not recognized in Query Loop
5 replies · Started by Artem on November 11, 2022
Hi,
Hope you are well.
I'd like my posts, with a "Link" post format, to open in a new tab. The solution provided by Ying works fine for archive pages:
https://generatepress.com/forums/topic/link-post-format-open-in-new-window/
However it doesn't work for posts displayed via Query Loop. All posts open in the same tab. Apparently, post format is not recognized in Query Loop.
Could you please recommend a solution on how to force Query loop to see post format.
Many thanks.
Artem
Hi there,
where can i see this issue ?
Hi David,
Sorry for the delay.
The link is in the private info.
Thanks a lot,
Artem
Try this PHP Snippet:
add_filter( 'render_block', function( $block_content, $block ) {
if (
!is_admin()
&& 'link' === get_post_format()
&& ! empty( $block['attrs']['className'] )
&& 'my-custom-class' === $block['attrs']['className']
) {
$block_content = str_replace( '<a ', '<a target="_blank" ', $block_content );
}
return $block_content;
}, 10, 2 );
NOTE: this line: && 'my-custom-class' === $block['attrs']['className'] ) { change the my-custom-class to your own class. Then select the Headline block that has the link applied, and add that class to Advanced > Additional CSS Class(es)
Dear David,
You make people happy.
Thank you for you help.
All the best
Glad we can be of help :)