WordPress Post Thumbs Fallback


/ Published in: PHP
Save to your folder(s)

If post thumbnail doesn't exist, then get the first uploaded image. Change sizes as needed.


Copy this code and paste it in your HTML
  1. //add support in functions file
  2. add_theme_support( 'post-thumbnails');
  3. add_image_size( 'home-feature', 590, 265, true ); // Homepage Feature Image
  4.  
  5. //function to call first uploaded image in functions file
  6. function main_image() {
  7. $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image&order=desc');
  8. if($files) :
  9. $keys = array_reverse(array_keys($files));
  10. $j=0;
  11. $num = $keys[$j];
  12. $image=wp_get_attachment_image($num, 'large', true);
  13. $imagepieces = explode('"', $image);
  14. $imagepath = $imagepieces[1];
  15. $main=wp_get_attachment_url($num);
  16. $template=get_template_directory();
  17. $the_title=get_the_title();
  18. print "<img src='$main' alt='$the_title' class='frame' />";
  19. endif;
  20. }
  21.  
  22. //goes in template to call image
  23. <?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
  24. echo get_the_post_thumbnail($post->ID,array(590, 265));
  25. } else {
  26. echo main_image();
  27. } ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.