Home>Article>CMS Tutorial> How to use external link images as article thumbnails in WordPress

How to use external link images as article thumbnails in WordPress

王林
王林 Original
2019-11-07 10:44:11 3427browse

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;

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;

3. In the article loop Use them

if($image_src)echo '';

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); }

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();

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!

Statement:
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