How to Add External Link to Featured Image

How to add an external link to the featured image on the main post page Where someone will click on the featured image and a new external url will open in another window.so Let’s see how it can possible. Here is my code, Copy it and Add this code in your theme’s functions.php file of your active child theme (or theme).

Than edit your post and add feature image and select Custom Fields from Screen options drop down on your post page after that Add New Custom Field “ExternalUrl” and your URL in the value entry. See Your feature image of post will now link externally to the Link set.


add_filter('post_thumbnail_html','add_external_link_on_page_post_thumbnail',10);
    function add_external_link_on_page_post_thumbnail( $html ) {
    if( is_singular() ) {
            global $post;
            $name = get_post_meta($post->ID, 'ExternalUrl', true);
            if( $name ) {
                    $html = '<a href="' . ( $name ) . '" target="_blank" >' . $html . '</a>';
            }
    }
    return $html;
    }

Leave a Comment

Scroll to Top