如何在 WordPress 上自动设置特色图片

关于timthumb.php不再支持或维护的介绍:https://code.google.com/archive/p/timthumb/

现在在用的缩略图的特性是需要从WordPress文章里的特色图像来调取裁剪的,而如果每次还要手动去设置一张实在是过于繁琐了,自然是在 WordPress 上自动设置特色图片来得方便很多哈,直接上代码

//自动添加特色图像
function huitheme_auto_set_featured_image() {
   global $post;
   $featured_image_exists = has_post_thumbnail($post->ID);
      if (!$featured_image_exists)  {
         $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);}
         }
      }
}
add_action('the_post', 'huitheme_auto_set_featured_image');

插入到functions.php即可,教程到此结束。

需要说明的是,这个操作是让文章内的图片自动设置一张图片为特色图像的操作,并不是对特色图像或文章内图像进行裁剪并生成缩略图的操作。

版权声明:
作者:wangluo
链接:https://codeftp.com/2025/02/06/%e5%a6%82%e4%bd%95%e5%9c%a8-wordpress-%e4%b8%8a%e8%87%aa%e5%8a%a8%e8%ae%be%e7%bd%ae%e7%89%b9%e8%89%b2%e5%9b%be%e7%89%87/
来源:源码分享网
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>