Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of automatically extracting the first picture in the content as a thumbnail by Wordpress

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the Wordpress automatic extraction of the first picture in the content as a thumbnail method, I hope you will learn a lot after reading this article, let's discuss it together!

How does Wordpress automatically extract the first picture in the content as a thumbnail?

Sometimes we publish articles without thumbnails, we want the system to automatically extract the first picture in our published articles as thumbnails, here I will introduce the Wordpress automatic extraction of the content of the first picture as a thumbnail method, students who need to know can refer to.

In php, we can use preg_match and preg_match_all to get the addresses of all the pictures in the content.

Step 1: add the following function to the function.php of the theme template, as follows:

The code is as follows:

/ / the function function catch_that_image () {global $post, $posts; ob_start (); ob_end_clean (); preg_match ('/ / iframes, $post- > post_content, $matche); if ($matche [1]) return $matche [1]; / / otherwise, take the default image return 'default.gif';}

Step 2: use this function directly where wordpress needs to call thumbnails, as in the following example, the code is as follows:

The code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report