1

I'v created a custom thumbnail size (in my function.php) called 'thumb-bookmark' and wanted to add a permalink to all of them but I don't know how to target only this specific size with this code:

function thumb_bookmark_html( $html, $post_id, $post_image_id, $size ) {
    $html = '<a class="MyClass" href="' . get_permalink( $post_id ) . '" alt="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;   
}
add_filter( 'post_thumbnail_html', 'thumb_bookmark_html', 10, 3 );

I've tried to add something like

$size = get_the_post_thumbnail('thumb-bookmark');

but, as I'm a noob beginner with php, it doens't worked of course :p

I've been searching on Google for hours without successs so any help will be much apreciate :) Thanks

1

You're getting the size as the $size as the 4th argument of your callback function. You just need to use an if statement to check it:

function thumb_bookmark_html( $html, $post_id, $post_image_id, $size ) {
    if ( $size === 'thumb-bookmark' ) {
        $html = '<a class="MyClass" href="' . get_permalink( $post_id ) . '" alt="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    }

    return $html;   
}
add_filter( 'post_thumbnail_html', 'thumb_bookmark_html', 10, 4 );

Also note that I changed 3 to 4 in the add_filter() function. This ensures that I get all 4 arguments in the callback function.

1
  • Seems obvious now I see your code. Many thanks, it works perfectly. I'd like to upvote for your answer but my "reputation" here doesn't allow me to do it yet ;) – dragoweb Nov 6 '17 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.