• Resolved Connor Crosby

    (@ccmovies)


    I am making a theme and want to have the featured image link to the post. Currently, I have it so if there is a featured image it will display it. If not, it will display nothing (as shown below).

    <?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    } else {
    // the current post lacks a thumbnail
    } ?>

    I do not want to put a link around this code and have it display a link when there is no image. How can I do it so if there is a thumbnail then it will permalink the image. I tried this code but it doesn’t work:

    <?php if ( has_post_thumbnail() ) {
    echo '<a href="'.the_permalink().'">'.the_post_thumbnail().'</a>';
    } else {
    // the current post lacks a thumbnail
    } ?>

    Instead it shows the link in plain text besides the image.

    PS: I am not very good with PHP.

Viewing 1 replies (of 1 total)
  • Thread Starter Connor Crosby

    (@ccmovies)

    Never mind, I figured it out by myself. If anyone is looking for the answer, use this code:

    <?php if ( has_post_thumbnail() ) { ?>
    < a href"<? the_permalink(); ?>"><? the_post_thumbnail(); ?></ a>
    <?php } else { ?>
    <!-- HTML goes here -->
    <?php } ?>

    Note: remove spaces in link tag. Also note not to use <?php ?> inside the code (only use <? ?>)

Viewing 1 replies (of 1 total)
  • The topic ‘Permalinking Featured Image using PHP’ is closed to new replies.