So I have this function that is working great to pull a default thumbnail if there is no featured thumbnail. But, I want to combine it with some code to pull a thumbnail based on the posts category. I have both working codes but im unsure how to combine the two properly, syntax wise. Here is my function:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default-blog-image.jpg";
}
return $first_img;
}
Now instead of a static image I want to get this code into the $first_img variable.
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg
This will allow me to add simply upload an image with the category name as a title like new.jpg. Then anything in the category named news will use this thumbnail. Thanks for any help.