Site logo

Archived topic

Add rel nofollow to featured image

5 replies · Started by Phuoc on December 6, 2019

Viewing posts 1–6 of 6

Hello support,

I need help to add rel nofollow to featured image link in Category.
I don't want to remove featured image link, only make it nofollow.
Thanks

Hi there,

try this PHP snippet:

add_filter( 'post_thumbnail_html', 'db_post_thumb_nofollow', 10, 3 );
 
function db_post_thumb_nofollow( $html, $post_id, $post_image_id ) {
 
  $html = '<a href="' . get_permalink( $post_id ) . '" rel="nofollow" >' . $html . '</a>';
  return $html;
 
}

Adding PHP:
https://docs.generatepress.com/article/adding-php/

Reference:
https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/#linking-to-post-permalink-or-larger-image

Thanks David,

The featured image link in category nofollow, but this code insert future image link in in single post too

Try:

add_filter( 'post_thumbnail_html', 'db_post_thumb_nofollow', 10, 3 );
 
function db_post_thumb_nofollow( $html, $post_id, $post_image_id ) {
  if ( is_single() || is_page() || is_front_page() ) {
    return $html;
  } else {
    $html = '<a href="' . get_permalink( $post_id ) . '" rel="nofollow" >' . $html . '</a>';
    return $html;
  }
}

featured image of Homepage still have link

Code updated here

This archived topic is closed to new replies.