• naysilva

    (@naysilva)


    Hi there,

    I want to set a default ‘Featured Image’ for every category that I have.

    In my functions.php file I have the following code:-

    /* ---------------FEATURED POST IMAGE------------------------*/
        function default_category_featured_image() {
        global $post;
        $featured_image_exists = has_post_thumbnail($post->ID);
        if (!$featured_image_exists) {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    
        if ($attached_image) {
        foreach ($attached_image as $attachment_id => $attachment) {
        set_post_thumbnail($post->ID, $attachment);
        }}
        else if ( in_category('2') ) {
        set_post_thumbnail($post->ID, '112');
        }
        else if ( in_category('3') ) {
        set_post_thumbnail($post->ID, '115');
        }
        else if ( in_category('4') ) {
        set_post_thumbnail($post->ID, '113');
        }
        else if ( in_category('8') ) {
        set_post_thumbnail($post->ID, '114');
        }
        else {
        set_post_thumbnail($post->ID, '0');
        }
        }
        }
        add_action('the_post', 'default_category_featured_image');

    Which is working for most of the Categories apart from WordPress. There are 6 posts at the minute that have ‘WordPress’ as the category and they all are using category ID ‘3’, but for some unknown reason, a few of the posts that have ‘WordPress’ as the category have the default featured image set, where as a few don’t have any default image set?

    Any idea why this is happening:-

    You can see the problem here:-

    https://www.web-tricks.co.uk

    From the homepage you can see the title ‘How to increase WordPress Memory Limit’ is working, and ‘Web-Tricks Top 10 Best Plugins for WordPress’ is now, but they have the same category ID – Any ideas?

Viewing 13 replies - 1 through 13 (of 13 total)
  • linux4me2

    (@linux4me2)

    It looks to me like the problem is that you’re using too many conditionals, which end up giving you unpredictable results. I like to keep things simple. Maybe something like this:

    function default_category_featured_image() {
      global $post;
      $category = get_the_category($post->ID);
      switch ($category) {
        case '2':
          set_post_thumbnail($post->ID, '112');
        break;
        case '3':
          set_post_thumbnail($post->ID, '115');
        break;
        case '4':
          set_post_thumbnail($post->ID, '113');
        break;
        case '8':
          set_post_thumbnail($post->ID, '114');
        break;
        default:
          set_post_thumbnail($post->ID, '0');
      }
    }
    add_action('the_post', 'default_category_featured_image');

    Thread Starter naysilva

    (@naysilva)

    Thanks linux4me2 – I’ve just updated my functions.php to your post but I’m still having the same problem, no idea what’s going on. Any other suggestions?

    linux4me2

    (@linux4me2)

    Hmmm…

    Are the posts that are giving you weird results in more than one category?

    Thread Starter naysilva

    (@naysilva)

    I’ve set the featured image manually as the WordPress one for now which works, I’ve left one of the WordPress category unset that is still not working which is the following post; ‘Web-Tricks Top 10 Best Plugins for WordPress’

    Thread Starter naysilva

    (@naysilva)

    There are two posts that have the category ‘WordPress’ and are not working, one has the following:-

    Categories: WordPress
    Tags: Plugins, WordPress

    The other has these:-

    Categories: WordPress
    Tags: WordPress

    Yet theres another post with the same categories/tags as above, and its working.

    It’s pretty random. I thought initially it was the posts that had images within the post as all the others that are working don’t have images, I tried removing them and this didn’t resolve it.

    linux4me2

    (@linux4me2)

    Try this instead:

    function default_category_featured_image() {
      global $post;
      $category = get_the_category($post->ID);
      switch ($category[0]->term_id) {
        case '2':
          set_post_thumbnail($post->ID, '112');
        break;
        case '3':
          set_post_thumbnail($post->ID, '115');
        break;
        case '4':
          set_post_thumbnail($post->ID, '113');
        break;
        case '8':
          set_post_thumbnail($post->ID, '114');
        break;
        default:
          set_post_thumbnail($post->ID, '0');
      }
    }
    add_action('the_post', 'default_category_featured_image');

    Thread Starter naysilva

    (@naysilva)

    Updated to the above, unfortunately it’s still the same ?? – is it possible to use something like $post-> ‘name’ i.e. ‘WordPress’

    linux4me2

    (@linux4me2)

    I changed the code a little by editing it above and you may have missed my changes, make sure you’re using this line:

    switch ($category[0]->term_id) {

    It’s better to use the IDs if you can, in case you change the category names down the line.

    Make sure you don’t have two “WordPress” categories set up. That would cause this problem because each would have a different ID.

    To troubleshoot this, it may be necessary to output the category for each article so you can see why you’re getting odd results.

    linux4me2

    (@linux4me2)

    The other thing that may be tripping us up is multiple categories. I’m just checking the first one in that code. If a post is assigned to more than one, which doesn’t appear to be the case, the code could fail.

    Thread Starter naysilva

    (@naysilva)

    linux4me2 – I copied and replaced the whole code you posted.

    I went into permalinks and changed it to show the post ID, and when I click on ‘WordPress’ for the none-working posts it does say ID = 3.

    I’ve also checked Posts –> Categories, and there is definitely only one Category called ‘WordPress’

    All posts only have 1 defined Category, some have 1 or 2 tags.

    I’ll try making a new post with the same content and see if that works.

    Thread Starter naysilva

    (@naysilva)

    Just to add, I’ve just added 3 posts with ‘WordPress’ as the category, one which was copied, one without the image, and one with just a title and one word as the post. All 3 posts with just ‘WordPress’ as the category are not displaying any image. I’m really unsure as to why this is not working now.

    Take a look here; https://web-tricks.co.uk/?cat=3

    You can see they are all in the same category, some showing featured image, some not.

    Thread Starter naysilva

    (@naysilva)

    Okay I have no idea why that wasn’t working but I installed the following plugin; ‘Default Thumbnail Plus’

    You can set a default image if there is no featured image set, and set an image for each category within the control panel. This appears to have fixed the problem and I guess this way it is easier to manage.

    linux4me2

    (@linux4me2)

    I am stuck too. I wonder if it would have worked had you put it in the loop instead of in the functions.php? If you do end up figuring out what was wrong, please post so I know too.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘WordPress – Setting a default 'featured image' for each Catergory’ is closed to new replies.