Advertisement
Guest User

Untitled

a guest
Dec 13th, 2013
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. if ( ! function_exists( 'set_my_featured_image' ) ) {
  2. add_action( 'save_post', 'set_my_featured_image' );
  3. function fb_set_featured_image() {
  4. $postID = get_the_ID();
  5. if ( has_post_thumbnail( $postID ) ) return NULL;
  6. if( get_post_type( $postID ) != 'post' ) return NULL;
  7.  
  8. $args = array(
  9. 'numberposts' => 1,
  10. 'order' => 'DESC', // ASC - pierwsza, DESC -ostatnia
  11. 'orderby' => 'menu_order', // rand - losowo
  12. 'post_mime_type' => 'image',
  13. 'post_parent' => $postID,
  14. 'post_status' => NULL,
  15. 'post_type' => 'attachment'
  16. );
  17.  
  18. $attached_image = get_children( $args );
  19. if ( $attached_image ) {
  20. foreach ( $attached_image as $attachment_id => $attachment )
  21. set_post_thumbnail( $postID, $attachment_id );
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement