• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

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 then 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

  • How To Display Category Image In Genesis Child Themes
  • How To Auto Set Featured Image Post Thumbnails
  • Set Fallback Featured Image For Each Category in Genesis

Featured Image

Reader Interactions

Comments

  1. Greg says

    July 21, 2014 at 8:04 pm

    just on main home post page

    Thanks for the help by the way

    Log in to Reply
    • Brad Dalton says

      July 21, 2014 at 8:21 pm

      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?

      Log in to Reply
  2. Greg says

    July 21, 2014 at 3:18 pm

    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

    Log in to Reply
    • Brad Dalton says

      July 21, 2014 at 4:21 pm

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

      Log in to Reply
  3. Greg says

    July 21, 2014 at 12:45 pm

    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

    Log in to Reply
    • Brad Dalton says

      July 21, 2014 at 2:16 pm

      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?

      Log in to Reply
      • Greg says

        July 21, 2014 at 2:50 pm

        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

        Log in to Reply
        • Brad Dalton says

          July 21, 2014 at 3:00 pm

          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.

          Log in to Reply
          • Greg says

            July 21, 2014 at 3:12 pm

            And that solved it. Thank you

            just need to tweek image sizes. 🙂

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

          • Brad Dalton says

            July 21, 2014 at 4:18 pm

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

      • Greg says

        July 21, 2014 at 3:00 pm

        after looking around in functions, could this be a problem

        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 );
        
        	
        }
        Log in to Reply
        • Brad Dalton says

          July 21, 2014 at 4:20 pm

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

          Log in to Reply
          • Greg says

            July 21, 2014 at 4:31 pm

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

          • Brad Dalton says

            July 21, 2014 at 5:25 pm

            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?

          • Greg says

            July 21, 2014 at 7:54 pm

            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.

          • Brad Dalton says

            July 21, 2014 at 7:58 pm

            Do you mean on single posts or archives?

        • Greg says

          July 21, 2014 at 8:14 pm

          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

          Log in to Reply
        • Greg says

          July 21, 2014 at 10:16 pm

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

          good advise.

          Thank you

          Log in to Reply
          • Brad Dalton says

            July 21, 2014 at 10:24 pm

            Sounds like you got it resolved?

          • Greg says

            July 22, 2014 at 3:43 am

            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 Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.