24 Responses

  1. Jeff
    Jeff July 6, 2012 at 8:30 am |

    Hey Justin,

    Good to see a new WordPress tutorial! It’s been awhile. I’ve also been enjoying the posts about country living. I myself have also been so fortunate as to find myself designing websites rurally by the ocean in Nova Scotia, so I can relate.

    Anyhow, out of curiosity, in Option 2 I was wondering if there was any particular reason you used get_stylesheet_directory_uri() as opposed to get_bloginfo('template_url') or get_template_directory_uri()? Also was wondering if there was any reason you choose to use trailingslashit() as opposed to putting the slash in yourself?

    These probably seem like pretty meaningless questions but I am always interested in the “right” way to do things in WordPress and I certainly trust you as a source for that.

    Thanks and take care!

    Reply
  2. Randy
    Randy July 6, 2012 at 10:42 am |

    I use my own function for getting a post thumbnail which :
    1. Looks for an assigned featured thumbnail,
    2. if nothing found, (optionally) grabs the first attached image to the post,
    3. or (optionally) inserts a default image.

    Of course, there are parameters depending on where I use it within my custom themes.

    On a (somewhat) related note…
    new clients often forget to click “use as featured image” once an image is uploaded. On the client’s defense, I find it misleading that when you use “Set Featured Image” as opposed to using the media uploader that the new image is not automatically assigned as the featured image upon uploading (or saving the post, or closing the popup, etc.).

    Thanks for the post_thumbnail_html tip. Now that I am far more into using hooks and filters I find it very hard to locate the one you need.

    Reply
  3. Thomas Scholz
    Thomas Scholz July 7, 2012 at 8:36 am |

    Option #2 (changing the theme) is not necessary.

    Just hook into 'get_post_metadata'. If the third argument is '_thumbnail_id' return TRUE.

    Reply
    1. Konstantin Kovshenin
      Konstantin Kovshenin July 10, 2012 at 11:05 pm |

      Thomas, you can’t just return true when get_post_thumbnail_id is expecting an integer for an attachment id. If you’re hooking into the get metadata functions, you’re expected to return valid results. In your case you can create an attachment post for default thumbnail and always return its id, if _thumbnail_id doesn’t exist in post meta.

      However Justin’s (second) method is much cleaner, easier to understand, and more future proof. Yours is quite dangerous. Cheers!

      Reply
  4. How to define default post thumbnails | WPCandy
  5. Maor Chasen
    Maor Chasen July 10, 2012 at 5:14 pm |

    Finally a honest and accurate article about default thumbnails. It really can’t get any simpler than that. Thanks Justin!

    Reply
  6. Konstantin Kovshenin
    Konstantin Kovshenin July 10, 2012 at 11:10 pm |

    Hi Justin, great stuff, as always! I don’t really see a good reason to use the first option you mentioned, other than for changing the markup for post thumbnails, for example a div with a background-image, instead of an img element. Second option is very straight to the point.

    Thanks!

    Reply
  7. Default Post Thumbnails in WordPress — Konstantin Kovshenin
  8. Azim
    Azim July 11, 2012 at 12:12 am |

    I’ve been using the second method for ages. Looks like I’m good at WP sometimes :) Now you prove me right :)

    Reply
  9. Michael
    Michael July 11, 2012 at 5:39 am |

    Thanks, going to be using the second method on some new project.

    Reply
  10. Paul
    Paul July 12, 2012 at 12:59 am |

    Hi Justin

    Can I just use the_post_thumbnail() in the theme without has_post_thumbnail() check ? This way it allows the first method to be able to work, is this okay ?

    Thanks

    Reply
  11. Michael Dennis
    Michael Dennis July 21, 2012 at 12:00 pm |

    Thanks for this Justin, I’ve been trying to get my head around advancing my theme development and posts like this really help. Great blog by the way – bookmarked.

    Reply
  12. Drew
    Drew July 23, 2012 at 3:29 pm |

    Great tutorial!

    Is there a way to have two default Thumbnails? For example if a user is male or female the thumbnail will reflect that? It might be more trouble than it’s worth, but thought I would ask.

    One other non related question. Do you or anyone else know anything about this site, discountwpthemes.com? It seem legit, I’ve just never used them before.

    Any information would be awesome. Thanks.

    Drew

    Reply
  13. Paul
    Paul October 18, 2012 at 5:15 am |

    How do you put a style class in the first example? I want to put a thumbnail in the page so I can have the image available when I post the article to facebook fan page. I want the class so I can move the thumb off the page with img.class{left:-999px}

    Thanks

    Reply
  14. shawn
    shawn November 17, 2012 at 9:12 pm |

    Here a modified version using a placehold.it


    function default_thumbnail($html) {
    global $_wp_additional_image_sizes;
    $isize = $_wp_additional_image_sizes;
    $w = $isize['post-thumbnail']['width'];
    $h = $isize['post-thumbnail']['height'];
    if (empty($html)) {
    $html = '';
    $html .= '';
    $html .='';
    }
    return $html;
    }

    Was wondering if there was a better way to do this??

    -shawn

    Reply
  15. shawn
    shawn November 17, 2012 at 9:15 pm |

    **sorry some of the above code got chopped here is a link to the gist

    https://gist.github.com/4102907

    Reply
  16. Random Joe
    Random Joe December 18, 2012 at 3:14 pm |

    All these methods are very deficient. The signature of the_post_thumbnail() is:

    the_post_thumbnail( $size, $attr );

    None of the solutions given above handle the $size and $attr parameters. For example, if the theme wants to display 320×200 thumbnails in one context, and 150×150 thumbnails in another context, then none of the methods given make it possible to automatically display the correctly cropped versions of the default thumbnail in both contexts.

    Reply
  17. WordPress:为文章设置默认的特色图像的方法总结(Default featured image) | 设计师品牌太阳镜
  18. Brad Dalton
    Brad Dalton April 11, 2013 at 3:17 pm |

    I tested these code snippets and they work however is this really a big problem:

    “saving a permanent default image (theme authors, please don’t do that).”

    Couldn’t you simply replace the image?

    Reply

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.