Several methods of thumbnail drawing in WordPress

Source: Internet
Author: User
Tags php file

The first picture can be found with the article ID. This can be written as follows: The user gets the first thumbnail, and returns an empty string if no pictures have been uploaded.


The following code is pasted into the function.php file for the topic:

The code is as follows Copy Code
Thumbnail capture
Add_theme_support (' post-thumbnails ');
Set_post_thumbnail_size (140, true);/set the size of the thumbnail
function Dm_the_thumbnail () {
Global $post;
A thumbnail that determines whether the article is set, and if so, it is displayed directly
if (Has_post_thumbnail ()) {
Echo ' <a href= '. Get_permalink (). ' " > ';
The_post_thumbnail ();
Echo ' </a> ';
else {//If the article does not have a thumbnail set, find out if the article contains pictures
$content = $post->post_content;
Preg_match_all ('//sim ', $content, $strResult, Preg_pattern_order);
$n = count ($strResult [1]);
if ($n > 0) {//If the article contains a picture, use the first picture as the thumbnail
Echo ' <a href= '. Get_permalink (). ' " ></a> ';
}else {//If there are no pictures in the article, use the default picture.
Echo ' <a href= '. Get_permalink (). ' " ></a> '";
}
}
}

Code comments are quite detailed, not too much to explain here. When you add a new article, there's a set thumbnail on the right, so it's OK.


Method Two

The code is as follows Copy Code

function Getfirstimage ($postId) {
$args = Array (
' Numberposts ' => 1,
' Order ' => ' ASC ',
' Post_mime_type ' => ' image ',
' Post_parent ' => $postId,
' Post_status ' => null,
' Post_type ' => ' attachment '
);
$attachments = Get_children ($args);

If no pictures are uploaded, return an empty string
if (! $attachments) {
Return ";
}

Gets the first picture in the thumbnail and assembles it into an HTML node to return
$image = Array_pop ($attachments);
$IMAGESRC = WP_GET_ATTACHMENT_IMAGE_SRC ($image->id, ' thumbnail ');
$IMAGEURL = $IMAGESRC [0];
$html = ' ';
return $html;
}

The code that is invoked is as follows.

The code is as follows Copy Code

$thumb = Getfirstimage ($post->id);
if (strlen ($thumb) > 0) {
Echo $thumb;
} else {
Show default picture or Don't do anything
}

Chapter feature picture (featured image) feature

WordPress 2.9, WordPress provides features of the article feature, you can set an upload for the article image as a feature picture, and can be set to a number of pictures in order to use in different environments. You can click the steps to call:

1. Add feature picture support for WordPress theme, and set the size and alias of feature picture.

The code is as follows Copy Code

Phpadd_theme_support (' post-thumbnails '); Support Feature picture feature

Add_image_size (' thumb ', 180, 180); Alias is thumb, size is 150x150 setting

Add_image_size (' recommend ', 120, 120); Alias is recommend, size is 120x120 setting

Add_theme_support (' post-thumbnails '); Support Feature picture feature

Add_image_size (' thumb ', 180, 180); Alias is thumb, size is 150x150 setting

Add_image_size (' recommend ', 120, 120); Alias is recommend, size is 120x120 setting

We can add the above code to the functions.php file, adding featured image support for the topic, and setting the 180x180 and 120x120 two sizes of pictures.

Where add_image_size is used to define a feature picture size, refer to the WordPress Codex, in fact it has 4 parameters.

1th parameter: The size alias of the feature picture, used to invoke thumbnails of different sizes.

2nd parameter: The width of the picture

3rd parameter: height of the picture

4th parameter: The parameter is a Boolean value that specifies how the picture is trimmed. The default is False.

If true, the picture is processed at a larger compression scale, and the extra part is trimmed off. For example now have picture 900x600, request compress into 150x150 picture, then will compress picture into 225x150 picture first, cut into 150x150.

If False, the picture is processed at a smaller compression scale. For example now has the picture 900x600, requests compresses into the 150x150 picture, then will compress the picture into the 150x100 picture.

The figure below is a two thumbnail, the original is 1024x768, and the left thumbnail is add_image_size (' xxx ', ","), and the right image uses add_image_size (' xxx ', ","), and false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.