In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to automatically add feature images to WordPress articles. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
WordPress's feature images are a useful feature that allows you to add a thumbnail to each article list. However, the feature image needs to be manually added when editing the article, and the following code can automatically set the first picture in the article as the feature image.
Add the following code to the functions.php of the current theme:
Function wpforce_featured () {global $post; $already_has_thumb = has_post_thumbnail ($post- > ID); if (! $already_has_thumb) {$attached_image = get_children ("post_parent=$post- > ID&post_type=attachment&post_mime_type=image&numberposts=1") If ($attached_image) {foreach ($attached_image as $attachment_id = > $attachment) {set_post_thumbnail ($post- > ID, $attachment_id);} / / end functionadd_action ('the_post',' wpforce_featured'); add_action ('save_post',' wpforce_featured') Add_action ('draft_to_publish',' wpforce_featured'); add_action ('new_to_publish',' wpforce_featured'); add_action ('pending_to_publish',' wpforce_featured'); add_action ('future_to_publish',' wpforce_featured')
What if there is no picture in the current article, but you want to display a default thumbnail? you can modify the above code and call an image in the library as the default thumbnail:
Function wpforce_featured () {global $post; $already_has_thumb = has_post_thumbnail ($post- > ID); if (! $already_has_thumb) {$attached_image = get_children ("post_parent=$post- > ID&post_type=attachment&post_mime_type=image&numberposts=1") If ($attached_image) {foreach ($attached_image as $attachment_id = > $attachment) {set_post_thumbnail ($post- > ID, $attachment_id);}} else {set_post_thumbnail ($post- > ID, '414');} / / end functionadd_action ('the_post',' wpforce_featured') Add_action ('save_post',' wpforce_featured'); add_action ('draft_to_publish',' wpforce_featured'); add_action ('new_to_publish',' wpforce_featured'); add_action ('pending_to_publish',' wpforce_featured'); add_action ('future_to_publish',' wpforce_featured')
The number 414 is the ID number of a picture attachment in the library.
Prompt
The above code is just a technical article, which may affect the feature images added before, so don't easily experiment on your own website.
Characteristic images are only suitable for users who do not care about space traffic and size, because each picture will be cut into multiple thumbnails of different sizes to facilitate calling in different locations, the most important thing is that it does not support outer chain, which is a waste of space.
Thank you for reading! On how to automatically add characteristic images for WordPress articles to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
# 1.57## [PROD #] telnet 109.120.1.86 7575Trying...Connected to 109.1
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.