WordPress自动添加设置调用特色图像,缩略图

教程介绍

这是一篇为WordPress自动调用特色图像的代码,现在很的主题都自带了这功能,但是也有部分主题没有,比如DUX主题,那么就造成了一个问题之前网站的缩略图可以显示换了某个主题后就不显示了,需要设置特色图像,但是我文章很多一篇一篇的设置太麻烦了,所以就有了这篇教程。
将以下代码添加到当前使用主题的functions.php文件中即可。

代码预览

  1. function autoset_featured() {
  2.           global $post;
  3.           $already_has_thumb = has_post_thumbnail($post->ID);
  4.               if (!$already_has_thumb)  {
  5.               $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  6.                           if ($attached_image) {
  7.                                 foreach ($attached_image as $attachment_id => $attachment) {
  8.                                 set_post_thumbnail($post->ID, $attachment_id);
  9.                                 }
  10.                            }
  11.                         }
  12.       }  //end function
  13. add_action('the_post', 'autoset_featured');
  14. add_action('save_post', 'autoset_featured');
  15. add_action('draft_to_publish', 'autoset_featured');
  16. add_action('new_to_publish', 'autoset_featured');
  17. add_action('pending_to_publish', 'autoset_featured');
  18. add_action('future_to_publish', 'autoset_featured');

评论2

评论前必须登录!

  1. #0
    已经把代码加入functions.php中了,可是并没有效果啊。 主题:DUX 文章中的图片是网络图片,文章也可以正常显示
    龙咕咕2015-08-14 14:37:54