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

[Black Friday] Gestion DBI: Los Angeles/UK VPS, 128MB RAM, $4/year

Gestion DBI is a stable merchant with good servic...

DMIT 1GB RAM 1Gbps Bandwidth Hong Kong HK-A KVM VPS Review

Details: DMIT: $9/month/512MB memory/10GB SSD spa...

Recommended domain name parking station – bodis

For no reason, the account was cancelled. Not rec...

Verelox: 20€/month/4GB RAM/500GB HDD/10TB traffic/Netherlands

Verelox, a hosting provider founded in 2015, has ...

Kloxo-MR vps panel

Kloxo-MR is perfect and pleasing both in terms of...

$5/month/512M memory/20G SSD space/1000G traffic/KVM: DigitalOcean

DigitalOcean is a company founded in NYC in 2012....

Godaddy Economy Web Hosting $1/month

The configuration of Godaddy Economy Web Hosting ...

Light Hosting: $10/month/4GB RAM/100GB storage/unlimited bandwidth/KVM/Malaysia

LightHosting, a Malaysian hosting provider, was e...

RamNode latest 10% discount coupon code

RamNode is stable, as we all know, and it is reco...

AlphaSSL Wildcard SSL Certificate*5 years 200 yuan

AlphaSSL Wildcard Certificates AlphaSSL Wildcard ...