Home>

the_post_thumbnailthe_post_thumbnail is mainly used to print the thumbnail set in the post in wordpress.The get_the_post_thumbnail function can return the html code you need as a string.

Uses of the_post_thumbnail function

the_post_thumbnail ($size, $attr)

Function parameter

$size refers to the type of thumbnail i want. The default is "post-thumbnail", which is the featured image. $attr Attribute setting in image img tag.

the_post_thumbnail function declaration

/ **
 * display post thumbnail.
 *
 * @since 2.9.0
 *
 * @param int $size optional. image size. defaults to "post-thumbnail", which theme sets using set_post_thumbnail_size ($width, $height, $crop_flag) ;.
 * @param string | array $attr optional. query string or array of attributes.
 * /
function the_post_thumbnail ($size="post-thumbnail", $attr="") {
 echo get_the_post_thumbnail (null, $size, $attr);
}
get_the_post_thumbnail function declaration
 * retrieve post thumbnail.
 *
 * @since 2.9.0
 *
 * @param int $post_id optional. post id.
 * @param string $size optional. image size. defaults to "post-thumbnail".
 * @param string | array $attr optional. query string or array of attributes.
 * /
function get_the_post_thumbnail ($post_id=null, $size="post-thumbnail", $attr="") {
 $post_id=(null === $post_id)?get_the_id ():$post_id;
 $post_thumbnail_id=get_post_thumbnail_id ($post_id);
 $size=apply_filters ("post_thumbnail_size", $size);
 if ($post_thumbnail_id) {
 do_action ("begin_fetch_post_thumbnail_html", $post_id, $post_thumbnail_id, $size);// for "just in time" filtering of all of wp_get_attachment_image () "s filters
 if (in_the_loop ())
  update_post_thumbnail_cache ();
 $html=wp_get_attachment_image ($post_thumbnail_id, $size, false, $attr);
 do_action ("end_fetch_post_thumbnail_html", $post_id, $post_thumbnail_id, $size);
 } else {
 $html="";
 }
 return apply_filters ("post_thumbnail_html", $html, $post_id, $post_thumbnail_id, $size, $attr);

set_post_thumbnail_sizeThe set_post_thumbnail_size function is a function for setting the size of a featured image in wordpress and a simple application of the add_image_size function.In order to better highlight the use of featured images, This function has been available in wordpress since version 2.9.0.

Use of set_post_thumbnail_size function

Similar to the use of the add_image_size function,But this function is only set for characteristic images.

set_post_thumbnail_size ($width, $height, $crop)

Detailed parameters

$width image width $height image height $crop whether to crop the image to its height and width

Examples

set_post_thumbnail_size (100,0, true);

Note:When either height or width is 0, wp will automatically adapt to the other value for thumbnail generation.

Function declaration

/ **
 * registers an image size for the post thumbnail
 *
 * @since 2.9.0
 * /
function set_post_thumbnail_size ($width=0, $height=0, $crop=false) {
 add_image_size ("post-thumbnail", $width, $height, $crop);
php
  • Previous Android implements SMS encryption (send encrypted SMS, decrypt local SMS)
  • Next MongoDB most basic command quick notes
  • Trends