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

$1.69/month/20G space/200G traffic virtual host——ServNode

ServNode is a newly established hosting provider....

Free top-level domain .tk/.ml/.cf

Freenom http://www.freenom.com Freedom Registry h...

Krypt: $15/month/2GB memory/40GB space/2TB traffic/VMware/Los Angeles

Krypt, an American hosting provider, has its own ...

AlphaRacks: $6.99/year/128MB/10GB space/500GB traffic/1 IP/OpenVZ/Los Angeles

AlphaRacks has 4 more special vps, the price is h...

VMHaus: $12.5/year/256MB memory/5GB NVMe space/1TB bandwidth/KVM/UK

vmhaus, introduced several times. There are 2 dis...

VMHaus: $15/year/256MB memory/5GB NVMe space/1TB bandwidth/KVM/UK

vmhaus, which I have introduced once before, has ...

drserver: $8.48/year/256MB memory/5GB SSD space/250GB bandwidth/Xen/Dallas

drserver, a stable merchant. Cyber ​​Monday deals...