Post Thumbnail Linking to the Post Permalink

Note: Don’t use these two examples together in the same Theme.


example 1. To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme’s template files:


<?php if ( has_post_thumbnail()) : ?>

<a href=”<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>” >

<?php the_post_thumbnail(); ?>


<?php endif; ?>

example 2. To link all Post Thumbnails on your website to the Post Permalink, put this in the current Theme’s functions.php file:


add_filter( ‘post_thumbnail_html’, ‘my_post_image_html’, 10, 3 );


function my_post_image_html( $html, $post_id, $post_image_id ) {


$html = ‘<a href=”‘ . get_permalink( $post_id ) . ‘” title=”‘ . esc_attr( get_post_field( ‘post_title’, $post_id ) ) . ‘”>’ . $html . ‘</a>’;

return $html;



Post Thumbnail Linking to Large Image Size

This example links to the “large” Post Thumbnail image size and must be used within The Loop.



if ( has_post_thumbnail()) {

$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘large’);

echo ‘<a href=”‘ . $large_image_url[0] . ‘” title=”‘ . the_title_attribute(‘echo=0’) . ‘” >’;


echo ‘</a>’;



Styling Post Thumbnails

Post Tumbnails are given a class “wp-post-image”. They also get a class depending on the size of the thumbnail being displayed You can style the output with these CSS selectors:







You can also give Post Thumbnails their own class.

Display the Post Thumbnail with a class “alignleft”:


<?php the_post_thumbnail(‘thumbnail’, array(‘class’ => ‘alignleft’)); ?>

 » 转载请注明来源:网络蛀虫小窝 » 《wordpress缩略图功能》
 » 本站地址:http://www.gomoth.com