Home > CMS Tutorial > WordPress > body text

How to use external link images as article thumbnails in WordPress

王林
Release: 2019-11-07 11:14:11
Original
3328 people have browsed it

How to use external link images as article thumbnails in WordPress

Ideas:

1. There must be a way to determine the image address: the first image in the article, or use a custom column Add a custom value.

2. Call the determined picture in the foreground: use the function method or call the picture directly.

The implementation is as follows:

Premise:

It is best for any call to be in a LOOP loop, so that it can be easily Use $post value.

1. Call the first picture in the article: use $post->post_content to get the article content, and then use the matching method to get the src value of the first picture.

preg_match('//i',$post->post_content,$index_piclink);
if(count($index_piclink) >= 2)$image_src = $index_piclink[1];
if(!strstr($image_src,'http://'))$image_src = false;
Copy after login

2. Call a custom column: When writing an article, add a custom column with the noun post_thumb, and then create it using the address of the image as the value. For example, meta_key:post_thumb,meta_value:http://www.utubon.com/images/logo.png, and then call it through the following method:

$image_src = get_post_meta($post->ID,'post_thumb',true);
$image_src = trim($image_src) !== '' ? trim($image_src) : false;
Copy after login

3. In the article loop Use them

if($image_src)echo '';
Copy after login

4. Make them into functions

function get_thumb_src($size = 'thumbnail',$first_pic_in_ctonte = true){
global $post;
$image_src = '';
if(function_exists('has_post_thumbnail') && has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_src = wp_get_attachment_image_src($image_id,$size);
$image_src = $image_src[0];
}else{
$image_src = get_post_meta($post->ID,'post_thumb',$single=true);
if(!$image_src && $first_pic_in_ctonte){
preg_match('//i',$post->post_content,$index_piclink);
if(count($index_piclink) >= 2)$image_src = $index_piclink[1];
if(!strstr($image_src,'http://'))$image_src =false;
}
}
return $image_src;
}
function the_thumb_src($size = 'thumbnail',$first_pic_in_ctonte = true){
echo get_thumb_src($size,$first_pic_in_ctonte);
}
Copy after login

This function (put it in functions.php) implements the selection of article thumbnails. If there is already a featured image, use the featured image. If not, check the post_thumb custom column. If there is no featured image, use the first image of the article. If the article does not have an image, return a false value. When using it, it is as follows:

if(get_thumb_src())the_thumb_src();
Copy after login

Recommended tutorial: wordpress tutorial

The above is the detailed content of How to use external link images as article thumbnails in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!