Archived topic
Link Post format - open in new window
5 replies · Started by Marcus on November 3, 2021
I’m trying to figure out how to have posts with the “link” post format open in a new window.
I'm guessing I need a filter of sorts where I add target=”_blank” within the a tag.
As it stands now, the archive page which lists the posts in this format correctly opens the link, instead of going to a detail / single post. The only thing I need now is just to have that link open in a new window.
Thanks in advance guys!
Hi Marcus,
Give this snippet a try:
add_filter( 'generate_get_the_title_parameters', function( $params ) {
if ( 'link' === get_post_format() ) {
$params = array(
'before' => sprintf(
'<h2 class="entry-title"%2$s><a target="_blank" href="%1$s" rel="bookmark">',
esc_url( generate_get_link_url() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
),
'after' => '</a></h2>',
);
}
return $params;
} );
Let me know :)
This is excellent Ying! Thank you.
However, my apologies, because I did forget one detail. I need to also have the code test for the presence of a specific category:
"/category/in-the-news/"
Could he category test be added to the if statement?
Try this one :)
add_filter( 'generate_get_the_title_parameters', function( $params ) {
if ( 'link' === get_post_format() && is_category( 'in-the-news' ) ) {
$params = array(
'before' => sprintf(
'<h2 class="entry-title"%2$s><a target="_blank" href="%1$s" rel="bookmark">',
esc_url( generate_get_link_url() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
),
'after' => '</a></h2>',
);
}
return $params;
} );
Perfect! You rock! #gpforever :)
You are welcome :)