28 Responses

  1. Jeff
    Jeff Published |

    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 Published |

    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 Published |

    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 Published |

      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 Published |

    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 Published |

    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 Published |

    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 Published |

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

    Reply
  10. Paul
    Paul Published |

    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 Published |

    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 Published |

    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 Published |

    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 Published |

    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 Published |

    **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 Published |

    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 Published |

    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
  19. sachin
    sachin Published |

    thaks its works

    Reply
  20. Jomy
    Jomy Published |

    Great tutorial! It’s just what I looking for. I’m a new Wordpress user and I’m looking for some like that to change my default tumbnail. Thanks!!

    Reply
  21. True
    True Published |

    Useful thanks – but can you elaborate on styling a bit.
    I’ve experimented with both but am lacking the styling options I get with the regular post thumbnail.

    For example, I set my custom thumbnail display in my functions.php

    add_image_size( 'tags-thumb', 80, 9999 );

    And in the loop, I access that styling using,

    In option 2, the thumbnail is treated differently. Could go down the route of using a css class, but it’s getting a bit long winded and untidy.

    Option one is preferable.
    But I want to treat the default thumbnail OR user selected thumbnails the same way.
    Is it possible to work my functions.php styling into option 1?

    Reply
  22. Lorenzo Lamas
    Lorenzo Lamas Published |

    Great trick! Thank you.
    I used the second option.
    Just one question: is it possible to modify/adapt this code to have “several” default thumbnail randomly used by default (4 or 5 pictures in a folder ussed randomly from one post to the other) ?
    => The objective is to avoid to have 20 times in a row the same thumbnail in a category.
    Thanks in advance !
    LL

    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.

css.php