• Resolved searay

    (@searay)


    I want to add an external HTML link to a post thumbnail using the filter, but the below code does not work for me. It is actually the page, not the post, can I use the post_thumbnail_html filter for page thumbnail?

    add_filter( 'post_thumbnail_html', 'my_post_link_html', 10, 3 );
    
    function my_post_link_html( $html, $post_id, $post_image_id ) {
    
    if ( $GLOBALS['ghostpool_affiliate_button_link'] ) {
    $affiliate_link = $GLOBALS['ghostpool_affiliate_button_link'];
    }
    
    $affiliate_target = apply_filters( 'gp_affiliate_link_target', '' );
    
    $html = '<a href="' . get_permalink( $affiliate_link ) . '" rel="nofollow" . target="' . $affiliate_target . '">' . $html . '</a>';
    
    return $html;
    
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi, @searay. Regarding this bit:

    $affiliate_link = $GLOBALS['ghostpool_affiliate_button_link'];
    

    Is that supposed to populate the variable with an actual URL? If so, then try changing this code:

    $html = '<a href="' . get_permalink( $affiliate_link ) . '" rel="nofollow" . target="' . $affiliate_target . '">' . $html . '</a>';
    

    to look like this instead:

    $html = '<a href="' . $affiliate_link . '" rel="nofollow" target="' . $affiliate_target . '">' . $html . '</a>';
    

    (I also removed an extra period you had just before the target attribute.)

    • This reply was modified 8 years, 2 months ago by girlieworks.
    Thread Starter searay

    (@searay)

    Thanks for reply, @girlieworks. That code still doesnt work. Maybe Im doing something wrong here. Here is the link to my site https://goo.gl/5ww6QK What I want to do is assign affiliate link to the thumbnail image (small square on the left in the head section). It should work the same as the Buy Now button on the right, but at the moment there’s no link attached to it. The $GLOBALS[‘ghostpool_affiliate_button_link’] stores the link that I enter when creating new page in admin. Could it be that Im trying to use the same variable for both button and thumbnail and this work only for button? Should I create another meta box for the thumbnail link?

    • This reply was modified 8 years, 2 months ago by searay.

    @searay, based on the HTML output I see for the thumbnail image on that page, I’m wondering whether:

    1) It’s not actually being generated by WordPress’s own the_post_thumbnail function, but rather, a custom function included in your theme (or perhaps a plugin), so your filter function is having no effect on it; OR

    2) Your theme (or a plugin) is already filtering the output for that WP function, in which case, you may need to change the priority parameter on your own filter so that it executes later than the theme’s filter function.

    Thread Starter searay

    (@searay)

    How can I test if the thumbnail image is generated by some other function than the_post_thumbnail?
    Isn’t 10 the max the filter priority can take?

    How can I test if the thumbnail image is generated by some other function than the_post_thumbnail?

    You’d want to look directly at the code which is generating the thumbnails to determine what function is being used.

    Isn’t 10 the max the filter priority can take?

    No, 10 is the default priority, but it’s not the max (which I think is platform-dependent, but still probably far larger than what you’d probably be using).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding an external HTML link to a page thumbnail’ is closed to new replies.