[Support request] Add rel nofollow to featured image

Home Forums Support [Support request] Add rel nofollow to featured image

Home Forums Support Add rel nofollow to featured image

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1096549
    Phuoc

    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

    #1096806
    David
    Staff
    Customer Support

    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

    #1104072
    Phuoc

    Thanks David,

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

    #1104083
    David
    Staff
    Customer Support

    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;
      }
    }
    #1104687
    Phuoc

    featured image of Homepage still have link

    #1104846
    David
    Staff
    Customer Support

    Code updated here

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.