Forums

[resolved] linking a the_post_thumbnail() result (3 posts)

  1. mrwolf2727
    Member
    Posted 1 year ago #

    I would think this would be easy, but I am having issues finding the right solution....

    My code is

    <?php the_post_thumbnail(array(200,150)); ?>

    However, it doesn't make the thumbnail clickable. I want to be able to click the thumbnail and have it take me to the attachments.php template to show the bigger version.

    I tried

    <a href="<?php post_permalink() ?>"><?php the_post_thumbnail(array(200,150)); ?></a>

    and

    <a href="<?php wp_get_attachment_url(get_post_thumbnail_id()) ?>"><?php the_post_thumbnail(array(200,150)); ?></a>

    and still not yielding the results I am looking for. Even changed the "Link To" field in the admin side of the thumbnail and nothing.

    Any help?

  2. mrwolf2727
    Member
    Posted 1 year ago #

    UPDATE: I found the get_attachment_link() function which should take me to the attachment.php page. However, nothing is returning from the function.

    Here is the code that I am using:

    <a href="<?php get_attachment_link(get_post_thumbnail_id($post->ID)) ?>"><?php the_post_thumbnail(array(200,150)); ?></a>
    <?php the_post_thumbnail_caption(); ?>
    <?php echo get_attachment_link(get_post_thumbnail_id($post->ID));?>

    The first get_attachment_link() outputs nothing, but the second one outputs the link that I want to use. I am assuming the_post_thumbnail() loads $post->ID, so how do I preload $post->ID before calling to the thumbnail?

  3. mrwolf2727
    Member
    Posted 1 year ago #

    ... that damn "echo" statement.

    Boy do I feel sheepish. In case anyone needs the right code in the future...

    <a href="<?php echo get_attachment_link(get_post_thumbnail_id($post->ID)) ?>">
    <?php the_post_thumbnail(array(200,150)); ?></a>

Topic Closed

This topic has been closed to new replies.

About this Topic