-1

I have a custom theme that for the latest posts displays the posts thumbnail. What I am looking to do is to check and if there is no thumbnail set then use the category's featured image. I could upload the images and use this:

if (!$photo) :
        $uploads = wp_upload_dir();
        $photo = '<div class="related-content-box__photo" style="background-image: url(' . $uploads['baseurl'] . '/2017/01/' . $category_name . '.jpg' . ')"></div>';
    endif;

What I don't like about this is that if I want to change one image of a category I have to upload all of them again and change the year/month of the folder.

So I have uploaded the images as featured images for the category themselves but how can I display them?

closed as off-topic by cybmeta, Dave Romsey, bueltge, kaiser Jan 11 '17 at 17:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • I suggests to use a plugin that adds featured images to taxonomies, including categories, so you can set a relationship between a category and an image and you can work with that relationship programmatically withing your theme. For example this one. Otherwise, I'm not sure what are you asking for. – cybmeta Jan 4 '17 at 12:27
  • 1
    How did you upload the images as featured category images? This is not a feature vanilla WordPress would have. – Howdy_McGee Jan 4 '17 at 18:49
  • I used the ACF custom field to upload a featured image. – Dragos Micu Jan 5 '17 at 10:09
0

The solution I found so far was to add featured image for categories as a custom field using ACF and then using the category ID to find and display each featured image.

Hope it helps anyone :)

0

If your theme is any good it uses get_the_post_thumbnail to retrieve the featured image. This function has a filter that you can use to detect whether there is a featured images and if not replace it. Like this:

add_filter ('post_thumbnail_html','wpse251260_thumbnail_html', 10, 5)

wpse251260_thumbnail_html ($html, $post_id, $post_thumbnail_id, $size, $attr) {
  if ( '' === $html ) {
    .. do stuff depending on category you retrieve with $post_id;
    }
   return $html
  }
  • Just in case: you can use add_term_meta to add a metabox to the category to set a featured image per category. – cjbj Jan 4 '17 at 13:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.