user12033678
user12033678

Reputation: 29

Using a variable to an external url in a php href

I'm trying to amend a Wordpress template so that the thumbnail of a single post links to an external url. I have collected the url in a custom field (report_link).

I've tried a bunch of combinations of dots and apostrophes and speech quotes, but I think I must be missing something fundamental.

<?php 
    if ( has_post_thumbnail() ) {                   
        $replink=the_field('report_link');
        if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
        echo "<a href='$replink'>$thumbnail</a>";
        }
    }
     ?>

When I try to use $replink in the href the link on the thumbnail is back to the same post's url.

I also tried:

    echo '<a href="'.$replink.'" target="_blank">Click</a>';

This returned the url in $replink printed to the screen and then a the word 'Click' which was linked back to the post page containing the link.

Upvotes: 0

Views: 230

Answers (1)

Techtic Solutions Inc
Techtic Solutions Inc

Reputation: 56

You can make use of below function to make it work.

if ( has_post_thumbnail() ) {
    $replink = get_field('report_link');
    if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
        echo "<a href='".$replink."'>".$thumbnail."</a>";
    }
}

Upvotes: 1

Related Questions