Request from a client. He wants his authors to be able to upload their image and then set it as featured, just like normal. The difference is that when setting it as featured, he wants the author to be able to choose 'small' or 'large' which will then be output appropriately on the front-end. How would you go about this? I'm well aware of how to create new image sizes in the theme, but how can I allow the author to select his image size when making his post?
2 Answers
You could add a meta box to the post add/edit screen below the featured image box to allow selection of size via a dropdown select menu. You could generate the list from available sizes (via get_intermediate_image_sizes
), or just use your small/large strings. The data would be saved as post meta and you would simply get_post_meta()
to get the selected size, then pass that result to the thumbnail function as the size parameter.
-
That is superb! I don't know why I didn't think of adding custom meta info to the posts. Thanks!– KevinCommented May 22, 2013 at 0:30
You can alter the content of that meta box, which is unusual.
Proof of concept:
function alter_thumb_box($content, $postID) {
return $content.'<input type="radio" name="thumb-size" value="" />';
}
add_filter( 'admin_post_thumbnail_html', 'alter_thumb_box', 1, 2 );
You just need to add your fields and then provide a function to save the data, similar to function in the Codex examples.
-
1This is great. Combining your answer with Milo's produced a very nice interface for selecting the image size. Much appreciated!– KevinCommented May 22, 2013 at 15:42