Add Default Featured Image For Each Post In A Category

In this tutorial, i’ll provide the code which sets a default post thumbnail/featured image for all posts in each category. This means all posts which don’t include a featured image, will automatically include the same featured image for that category.

First step is to create a default featured image for each category and upload them using the native Media library in WordPress.

You will then need to find the i.d for each category and the attachment i.d for each image.

Here’s an example:

Permalink: http://example.com/wordpress/?attachment_id=40380 category i.d 22

Permalink: http://example.com/wordpress/?attachment_id=40383 category i.d 23

Permalink: http://example.com/wordpress/?attachment_id=40382 category i.d 24

Each permalink can be viewed by going to Media > Library > Edit Image. Here’s a screenshot:

edit media

You can clearly see one category featured image which has been uploaded to the Media Library and the permalink for this image.

At the end of the permalink you can see the attachment i.d for this image which needs to be added to the code below along with the category i.d you want the image to display as your default category featured image.

Once you have all the i.d’s, you can then replace them in this code which needs to be added to the end of your child themes functions.php file.

You can add more else if statements to this code if you know what you’re doing.

The code also includes a default featured image which is set for all other posts not in the categories listed in the code. You can remove these 2 lines of code if you don’t want a global default featured image set for posts which you haven’t added a featured image:

else if ( in_category('56') ) {
set_post_thumbnail($post->ID, '40382');
}

Related Tutorials


Comments

20 responses to “Add Default Featured Image For Each Post In A Category”

  1. just on main home post page

    Thanks for the help by the way

    1. Brad Dalton Avatar
      Brad Dalton

      Looks like you have other issues there as the padding and margins have been modified.

      Are you still using that PHP code in your functions file to add featured images anywhere?

  2. Ok now I remember, I chose to bring that image inside the post content area on blog page.

    so now I have image left of title and same featured image in content area

    any idea on hiding the image left of title on a post

    1. Brad Dalton Avatar
      Brad Dalton

      Yes, remove the code which added it from your functions file.

  3. Hi Brad, I’ve got a good one for Ya, 🙂

    I have a Category as a menu item ‘article”. It shows the posts with the category Article and the read more.
    when I click read more I see the full post with an image.

    but in the category view no featured post image. I would like to add the featured image from the post to each in the category view. http://realestateradioshow.com/article/

    Im not a good coder but can do basic stuff also I use genesis eleven40 as my theme 3.9.1

    Thanks your your ideas

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Buddy

      If you’ve added it as a featured image, it should also display on all archives according to your Genesis > Theme Settings > Content Archive settings. If not, it may be because you added code which over rides this, is that the case?

      1. Don’t think so, I did add code to show featured images on main blog page and post pages, as eleven40 didn’t show by default.

        would the possible code be in functions of child.

        what might I be looking for. that may be commented out

        1. Brad Dalton Avatar
          Brad Dalton

          You don’t need to add code to display images on the blog page as the Genesis > Theme Settings > Content Archives control this for featured images so that’s where the problem may be.

          1. And that solved it. Thank you

            just need to tweek image sizes. 🙂

            by the way really enjoy the site. one of my favs

          2. Brad Dalton Avatar
            Brad Dalton

            Great! Give me a link if that’s the case. Thanks.

      2. after looking around in functions, could this be a problem

        [code]
        add_action( ‘genesis_entry_content’, ‘show_featured_image_single_posts’, 9 );
        function show_featured_image_single_posts() {

        if ( ! is_singular( ‘post’ ) ) {
        return;

        }

        $image_args = array(

        ‘size’ => ‘single-post-thumbnail’,
        ‘attr’ => array(
        ‘class’ => ‘alignright’,
        ),
        );

        genesis_image( $image_args );

        }
        [/code]

        1. Brad Dalton Avatar
          Brad Dalton

          Yes, not good code. Where did you get it?

          1. Don’t recall, feel like im scraping , with a hint of learning. lol

          2. Brad Dalton Avatar
            Brad Dalton

            If you only want to display the images in archives and not single posts, simply remove the PHP code you added to your functions file as its not needed.

            Or have i missed something?

          3. You will love this one,

            Although I am cool with having image on main, archives, and category view.

            the original image (same name as featured image) is acting like a second featured image.

            and for the life of me i cant figure out how to remove it.
            looked at functions code and don’t see it.

          4. Brad Dalton Avatar
            Brad Dalton

            Do you mean on single posts or archives?

        2. If I remove check box for the image in Genesis > Theme Settings > Content Archive settings.

          the second image is still there on home post page

        3. Took some of your advice last time we chatted, R&R each add-in till you find the problem.

          good advise.

          Thank you

          1. Brad Dalton Avatar
            Brad Dalton

            Sounds like you got it resolved?

          2. Turns out a auto featured image loader addin that worked when archive image is off wants to add a second featured image when archive image is on.

            needed to thin out the addins anyway lol.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.