自动为WordPress文章添加第一张图片为特色图片的代码

image

  1. function wpforce_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’, ‘wpforce_featured’);
  14. add_action(‘save_post’, ‘wpforce_featured’);
  15. add_action(‘draft_to_publish’, ‘wpforce_featured’);
  16. add_action(‘new_to_publish’, ‘wpforce_featured’);
  17. add_action(‘pending_to_publish’, ‘wpforce_featured’);
  18. add_action(‘future_to_publish’, ‘wpforce_featured’);
打赏

发布者

张 大鹏

北京理想国惠玩科技有限公司CEO

发表评论

电子邮件地址不会被公开。 必填项已用*标注