-
helpgeneratepress
I found this topic – https://generatepress.com/forums/topic/post-title-with-external-link/ and I was able to add an external link to the title. I wanted to know if I can do the same and add an external link to the featured image.
To clarify. The original topic only have the title and external link. My posts contains a featured imaged and a title and needs to go to an external link.
Other important information. Not all post needs an external link. Some links goes to the Post page.
-
Hi there,
that filter should work with an Image block too. Did you try adding the necessary CSS class to the Image block as well ?
-
helpgeneratepress
Hi David,
Yes, I added dynamic-title to the Image block.
-
Alvind
Hi there,
Could you provide a link to your site?
-
helpgeneratepress
I’ve provided the url to the site under the Private information section.
Under the homepage, please scroll down to the News and Events section.
The item in question is the first item. When you click on the title it will go to the external link. When you click on the image, it will go to the post page.
You can confirm that the css class dynamic-title is used in both the Headline Block and the Image Block by using inspect element.
Please let me know if you have any questions.
-
helpgeneratepress
Hi Alvind,
Did you get a chance to look at this?
Peace,
Konrad -
Hi Konrad,
Sorry that your last reply slipped through our support system.
I checked your site, and the class has been added correctly to the image block, but it’s not working which is strange.
I tested the code on my site and it works fine.
Can you disable all plugins except GB and GP Premium to test?
Let me know π
-
helpgeneratepress
Hi Ying,
I disabled all plugins expect for GB, GB Pro and GP Premium. The external link works on the Title but not the image.
I also removed all codes in functions.php except for the code provided in https://generatepress.com/forums/topic/post-title-with-external-link/
Here is the code for you reference:
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 ); 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 ); } $featured_image_url = get_the_post_thumbnail_url(); return $block_content; }, 10, 2 );
-
I tested on the image block as well, and the code absolutely works!
Can you provide an admin login and keep all plugins (except GB/GB Pro/GP Premium) deactivated?
Let me know π
-
helpgeneratepress
Please see the private information section.
-
Can you update GB plugin first?
-
It was not working for the image, because you have anther class added the image, the code only allows one additional class.
Try to change the code to this, which allows you to have multiple additional CSS classes:
add_filter( 'generateblocks_dynamic_url_output', function( $url, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'dynamic-title' ) !== false && 'link' === get_post_format() ) { $content = get_the_content(); $has_url = get_url_in_content( $content ); $url = ( $has_url ) ? $has_url :get_permalink() ; } return $url; }, 10, 2 ); add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && 'link' === get_post_format() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'dynamic-title' ) !== false ) { $block_content = str_replace( '<a ', '<a target="_blank" ', $block_content ); } $featured_image_url = get_the_post_thumbnail_url(); return $block_content; }, 10, 2 );
Let me know if this helps!
-
helpgeneratepress
That worked. Thank you
-
No Problem π
- You must be logged in to reply to this topic.