WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Permalinking Featured Image using PHP (2 posts)

  1. Connor Crosby
    Member
    Posted 2 years ago #

    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.

  2. Connor Crosby
    Member
    Posted 2 years ago #

    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 <? ?>)

Topic Closed

This topic has been closed to new replies.

About this Topic