I suggest that you install FPW Category Thumbnails plugin. It will cover every scenario mentioned in your question. It will save you from maintaining code in functions.php
, and will do it visually! The built-in FPW Post Thumbnails will add very powerful styling to your featured images (borders, shadows, margins, backgrounds, etc). You will be able to switch themes without losing its functionality!
Your code (if you want to use it) needs 24 changes to work. Replace:
if(!isset($_POST['_thumbnail_id'])) {
with
if(-1 == $_POST['_thumbnail_id']) {
and replace:
$cats = $post_categories->term_id;
with
$cats = $post_categories[0]->term_id;
In lines checking category id, change assign operator =
to comparison ==
.