- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
November 25, 2022 at 2:03 am #2432532
Lena
Hi. I have a bunch of blog posts with post format ‘link’. The posts only include a title and an external link. I’m now trying to show a list of the latest posts on the frontpage and I want the titles to link directly to the external site, but instead the links go to the posts. I tried with all different blocks like ‘Latest posts’ and ‘Loop query’ but I get the same result.
November 25, 2022 at 9:48 am #2433624David
StaffCustomer SupportHi there,
if you use a GB Query Loop block, and use a Headline Block to display the Title with a dynamic link set to the Post.
So that link will go to the single post… we’ll correct that with some code.With that block selected go to: Advanced > Additional CSS Class(es) and give it a class of:
dynamic-titleWith that all saved add this PHP Snippet to your site:
add_filter( 'generateblocks_dynamic_url_output', function( $url, $attributes ) { if ( ! empty( $attributes['className'] ) && 'dynamic-title' === $attributes['className'] && 'link' === get_post_format() ) { $content = get_the_content(); $has_url = get_url_in_content( $content ); $url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() ); } return $url; }, 10, 2 );That code will check if the post is the link format, and if it is and there is a URL in the posts content it will swap the link for you. If no link is found it will carry on linking to the post.
November 28, 2022 at 2:54 am #2438552Lena
Great. Thank you. Works perfect. Is there a way I can add target=”_blank” somewhere in the code to open the links in another tab/window?
November 28, 2022 at 6:22 am #2438846David
StaffCustomer SupportGood to hear that.
Add this snippet as well.add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && 'link' === get_post_format() && ! empty( $block['attrs']['className'] ) && 'dynamic-title' === $block['attrs']['className'] ) { $block_content = str_replace( '<a ', '<a target="_blank" ', $block_content ); } return $block_content; }, 10, 2 );November 29, 2022 at 2:32 am #2440606Lena
That works perfect, also! Thanks a bunch and happy holidays.
November 29, 2022 at 4:00 am #2440722David
StaffCustomer SupportGlad to hear that!!
-
AuthorPosts
- You must be logged in to reply to this topic.