Thursday, April 2, 2015

How to display wordpress recent posts of custom size image in widgets


Function Reference/get the post thumbnail

Description

Gets the Featured Image (formerly called Post Thumbnail) as set in post's or page's edit screen and returns an HTML image element representing a Featured Image, if there is any, otherwise an empty string.
Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See alsoPost Thumbnails.

Usage

 <?php echo get_the_post_thumbnail$post_id$size$attr ); ?> 

Parameters

$post_id
(integer) (Optional) Post ID.
Default: Post ID
$size
(string/array) (Optional) Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32).
Default: 'post-thumbnail'
$attr
(string/array) (optional) Query string or array of attributes. See wp_get_attachment_image.
Default: None
$default_attr = array(
 'src' => $src,
 'class' => "attachment-$size",
 'alt' => trim( strip_tags( $attachment->post_excerpt ) ),
 'title' => trim( strip_tags( $attachment->post_title ) ),
);

Return Values

Returns a string containing an HTML image element, or an empty string if no post thumbnail is found.

Examples

Sample Usage

<?php $pages = get_pages( array( 'child_of' => 1 ) ); ?> 
<ul>
 <?php foreach ( $pages as $page ) : ?>
  <li>
   <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
   <h1><?php echo apply_filters( 'the_title', $page->post_title, $page->ID ); ?></h1>
   <?php echo apply_filters( 'the_content', $page->post_content ); ?>
  </li>
 <?php endforeach; ?>
</ul>

Thumbnail Sizes

The default image sizes of WordPress are "thumbnail", "medium", "large" and "full" (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. Themes may also add "post-thumbnail". This is how you can use these default sizes with get_the_post_thumbnail():
// without parameter -> Post Thumbnail (as set by theme using set_post_thumbnail_size())
get_the_post_thumbnail( $post_id );                   

get_the_post_thumbnail( $post_id, 'thumbnail' );      // Thumbnail (Note: different to Post Thumbnail)
get_the_post_thumbnail( $post_id, 'medium' );         // Medium resolution
get_the_post_thumbnail( $post_id, 'large' );          // Large resolution
get_the_post_thumbnail( $post_id, 'full' );           // Original resolution

get_the_post_thumbnail( $post_id, array( 100, 100) ); // Other resolutions
Register new image sizes for Post Thumbnails with: add_image_size().
To set the default size for Post Thumbnails see: set_post_thumbnail_size().

Post Thumbnail Linking to the Post Permalink

This example shows the 5 latest Post Thumbnails linked to their Post permalink.
<?php 
$thumbnails = get_posts( 'numberposts=5' );
foreach ( $thumbnails as $thumbnail ) {
 if ( has_post_thumbnail( $thumbnail->ID ) ) {
  echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
  echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
  echo '</a>';
 }
}
?>

Post Thumbnail Linking to large Image Size

This example links to the "large" Post Thumbnail image size and must be used within The Loop.
<?php 
if ( has_post_thumbnail() ) {
 $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
 echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '" >';
 echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); 
 echo '</a>';
}
?>

Styling Post Thumbnails

Post Thumbnails 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:
img.wp-post-image
img.attachment-thumbnail
img.attachment-medium
img.attachment-large
img.attachment-full
You can also give Post Thumbnails their own class.
Give the Post Thumbnail a class "alignleft".
<?php echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'class' => 'alignleft' ) ); ?>

Notes

If the required add_theme_support( 'post-thumbnails' ); in the current theme's functions.php file is attached to a hook, it must be must be called before the init hook is fired. The init hook may be too late for some features. If attached to a hook, it must beafter_setup_theme.

Change Log

Since: 2.9.0

Source File

get_the_post_thumbnail() is located in wp-includes/post-thumbnail-template.php.

External Resources

Related

See also index of Function Reference and index of Template Tags.

No comments:

Post a Comment