WordPress theme add feature image features

WordPress from version 2.9 to support the article features image features, the use of wordpress features image function, will use the site more standardized, improve the page loading speed, how to make the theme support feature image function is very simple.
The first step, add the theme of the feature image support
Place the following code subject in the functions.php file:
// Add featured image function
add_theme_support (‘post-thumbnails’);
set_post_thumbnail_size (130, 100, true); // image width and height
Which the length of the picture can be modified.
Step 2, add the featured image call code
Add the following code to the appropriate location of the theme template, such as the archive archive archive.php:
<? php
if (has_post_thumbnail ()) {
// Show featured images
the_post_thumbnail ();
} else {
// Set the featured image
$ attachments = get_posts (array (
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘posts_per_page’ => 0,
‘post_parent’ => $ post-> ID,
‘order’ => ‘ASC’
));
if ($ attachments) {
foreach ($ attachments as $ attachment) {
set_post_thumbnail ($ post-> ID, $ attachment-> ID);
break
}
// Show featured images
the_post_thumbnail ();
}
}
The code states that if the feature image is not set manually, the “thumbnail” of the first picture attachment will be automatically called as the featured image and displayed.
Note: The WP function used in the code:
has_post_thumbnail ()
set_post_thumbnail ()
the_post_thumbnail ()
You can go to the official Codex to view detailed instructions and modify them as needed.
Calling a display feature image You can also use another method:
If you think that adding a feature image call code to the subject template main loop will look messy, you can add the following code to the theme functions.php file:
// featured images
add_filter (‘the_content’, ‘set_featured_image_from_attachment’);
function set_featured_image_from_attachment ($ content) {
global $ post;
if (has_post_thumbnail ()) {
// Show featured images
$ content = the_post_thumbnail (). $ content;
} else {
// Get and set the featured image
$ attachments = get_children (array (
‘post_parent’ => $ post-> ID,
‘post_status’ => ‘inherit’,
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘order’ => ‘ASC’,
‘orderby’ => ‘menu_order’
));
if ($ attachments) {
foreach ($ attachments as $ attachment) {
set_post_thumbnail ($ post-> ID, $ attachment-> ID);
break
}
// Show featured images
$ content = the_post_thumbnail (). $ content;
}
}
return $ content;
}
The code is basically the same as above, except that the_content () is filtered using get_children instead of get_posts ().