How to implement thumbnail of WordPress homepage log

How to implement thumbnail of WordPress homepage log


There are several ways to implement thumbnails in WordPress. The first is to use a plug-in, which is the simplest. The second is to use timthumb.php. The third is to add code. Aiba uses the third method, which is more customizable and less error-prone.
Functionally, the first picture in the document is called as the thumbnail. If there is no picture, a random picture is called.
Here are the methods:
1. Add code to functions.php in the theme folder

//Get article thumbnail
function dm_the_thumbnail() {
global $post;
if ( has_post_thumbnail() ) {
echo '<a href="'.get_permalink().'">';
$domsxe = simplexml_load_string(get_the_post_thumbnail());
$thumbnailsrc = $domsxe->attributes()->src;
echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
echo '</a>';
} else {
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'”]?(.+?)[\'”]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
$n = count($strResult[1]);
$random = mt_rand(1, 5);
if($n > 0){
echo '<a href="'.get_permalink()."><img src="'.$strResult[1][0].'" alt="'.get_the_title()."" title="'.get_the_title().""/></a>';
}else {
echo '<a href=”'.get_permalink().'”><img src=”'.get_bloginfo('template_url').'/images/random/oukan'.$random.'.jpg” alt=”'.get_the_title().'” title=”'.get_the_title().'”/></a>';
}
}
}
add_theme_support('post-thumbnails');
set_post_thumbnail_size(100, 140, true);

2. In the theme files index.php; category.php; archive.php

<?php the_content('(Continue reading..)'); ?>

Added in front

<?php dm_the_thumbnail(); ?>

the_content may also be the_excerpt.
3. Add the following CSS code to the theme file style.css.

.post .intro img {
float:left;
width:140px;
height:100px;
margin:12px 10px 8px 15px;
padding:4px;
border:1px solid #ccc;
position:relative;
z-index:3;
}

Among them, .post is the CSS of the log and related information, and .intro is the CSS of the_content.
Provide a random image (put it under images in the theme directory, remember to change the code if you put it elsewhere): random
Picture demonstration:

<<:  $25/year/128MB RAM/20GB storage/500GB bandwidth/KVM —— QuickPacket

>>:  $0.99/month/10GB space/300GB traffic virtual hosting - Network Panda

Recommend

HostHongKong:$9.87/month/512MB memory/40GB space/200GB traffic/KVM/Hong Kong

HostHongKong is a long-established hosting provid...

FortVPS: $6/month/6GB RAM/200GB storage/100TB bandwidth/OpenVZ/Canada

FortVPS, opened by Mexicans, mainly provides Open...

Backing up and restoring an OpenVZ VPS using vzdump

Preparation Now, let's assume that there are ...

Linode KVM VPS 2GB RAM SSD Hard Drive Japan Tokyo 2 Simple Review

Linode, the second data center in Japan is now op...

AlphaRacks: $10/year/2 CPUs/256MB/25GB/500GB/1 IP/OpenVZ

Previously, I introduced the 512MB version at $15...

RackLot: $9/month/1GB RAM/50GB storage/unlimited/100Mbps/KVM/UK

RackLot, a foreign merchant, seemed to have had a...

ITLDC 2GB RAM SSD Hard Drive Singapore KVM VPS Review

Details: ITLDC: €21/year/1GB RAM/10GB SSD space/u...