i’m trying to get a thumbnail to display if it exists within a div class, but it’s outputting code in unexpected ways (like the permalink is outside of the href)
what am i doing wrong?
<?php
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo '<div class="thumbnail"><a href="' . the_permalink() . '">' . $image[0] . '</a></div>';
} else {
echo '';
}
?>
results in
http://www.permalink.com/<div class="thumbnail"><a href="">http://www.mysite.com/wp_myblog/wp-content/uploads/2011/10/fretless-thumbnail1.jpg</a></div>
and no, i didn’t leave out any carrots, brackets, quotes or any other code. this is copy and past exactly how it is outputing
EDIT : THE FIX
i had to add some extra html since the fix only spit out the jpg url sans img tags. plus, it wasn’t displaying the correct image – it was showing the original jpg instead of the thumbnail version
<?php
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
echo '<div class="thumbnail"><a href="' . get_permalink() . '">' . '<img src="' . $image[0] . '"></a></div>';
}
?>
YAY!
The
the_permalinkfunction includes anechostatement already.Change it to
get_permalink, and it should work correctly:You don’t really need the
elsebit, either. It’s probably redundant.In fact, for a slightly more neater alternative, this would probably work (amended from my own code; just added the link):