• Resolved Stefan

    (@stefan83)


    Hi – I’m trying to link the featured image using the value from a custom field. This is where I’ve got so far but it only displays the image and not any links at all.

    <?php if(has_post_thumbnail())
    {
    	$image_url = the_post_thumbnail( 'blog' );
    	$image_url = $image_url[0];
    	$external_url = get_post_meta( $postID, 'Website:', true);
    
    	if ( $external_url ) {
    		echo '<a href="'.$external_url. '" target="_blank"><img src="'.$image_url.'" alt="" /></a>';
    	} else {
    		echo '<a href="'.get_permalink($postID).'"><img src="'.$image_url.'" alt="" /></a>';
    	}
    } ?>

    Any help would be great. Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What does the HTML output?

    <a><img.../></a>
    or
    <img.../>

    ?

    Thread Starter Stefan

    (@stefan83)

    its outputs

    <img.../>

    (with image)

    then

    <a href=""><img alt="" src=""></a>

    (with no image)

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    which of the output is not to your expectations?

    Thread Starter Stefan

    (@stefan83)

    Thanks for your help with this, anevins.

    I’m not sure why there is an image appearing without <a>

    And I’m not sure why the image is not appearing within the <a>

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I’d start with wrapping your code to check if the $image_url exists

    if ( $image_url ){
    
    if ( $external_url ) {
    		echo '<a href="'.$external_url. '" target="_blank"><img src="'.$image_url.'" alt="" /></a>';
    	} else {
    		echo '<a href="'.get_permalink($postID).'"><img src="'.$image_url.'" alt="" /></a>';
    	}
    }
    }

    Then I would look into why $image_url isn’t populating in those instances.

    for use as string, use get_the_post_thumbnail() https://codex.www.ads-software.com/Function_Reference/get_the_post_thumbnail
    also, get_the_post_thumbnail() has the full img html code.

    <?php if(has_post_thumbnail())
    {
    	$image_url = get_the_post_thumbnail( $postID, 'blog' );
    	$external_url = get_post_meta( $postID, 'Website:', true);
    
    	if ( $external_url ) {
    		echo '<a href="'.$external_url. '" target="_blank">'.$image_url.'</a>';
    	} else {
    		echo '<a href="'.get_permalink($postID).'">'.$image_url.'</a>';
    	}
    } ?>

    is $postID the ID of the current post?

    if not, try to use $post->ID

    Thread Starter Stefan

    (@stefan83)

    Got it. Thanks guys. Final code, for anyone interested is:

    <?php if(has_post_thumbnail())
    {
    	$image_url = get_the_post_thumbnail( $post->ID, 'blog' );
    	$external_url = get_post_meta( $post->ID, 'Website:', true);
    
    	if ( $external_url ) {
    		echo '<a href="https://'.$external_url. '" target="_blank">'.$image_url.'</a>';
    	} else {
    		echo '<a href="'.get_permalink($postID).'">'.$image_url.'</a>';
    	}
    } ?>

    Great, thanks a lot, Stefan 83.
    I use the Theme Sampression Lite and needed your Code to point directly to the websites of my clients.
    https://www.bocqbox.de

    Hi, sorry for the noob question but on what page do I insert this code and where?

    same question with jaminunit…….

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Linking featured image with custom field URL’ is closed to new replies.