It should not be if ( has_post_thumbnail(‘bild-lopsedel’));
It should be if ( has_post_thumbnail(‘$post->ID’));
The post has the thumbnail, not the thumbnail declaration has a thumbnail
For each image size you define, there will be a copy of that specific image created in each of the sizes that you define. If you make three image size definitions, and upload 10 images, you will end up with 30 images.
The trick is to make your IF based on the given template (or some other condition), rather than on the existence of an image. The images will always exist. The point to has_post_thumbnail is to define whether or not you’ve selected one to the featured image.
Thread Starter
yurini
(@yurini)
Thanks for your reply! Okay I think I get that.
But do you mean that even if I remove the custom “add_image_size” values, WordPress is always going to make three sizes of every image (beeing read from the Media options under Settings in the admin-panel) that is going to be stored in the wp-content/uploads folder? I supose you meant that it can affect the bandwith or at least the space on the server if WordPress generates alot of images that maybee not is necessary?
One way to solve this would maybee be with custom taxonomies I can imagine? For example something like this:
<?php
$post = $wp_query->post;
if ( has_term('bild-lopsedel', 'lopsedel', $post->ID ) ) the_post_thumbnail( 'bild-lopsedel', array( 'class' => 'bild-lopsedel' ));
elseif ( has_term( 'bild-lopsedel-hoger', 'lopsedel', $post->ID )) the_post_thumbnail( 'bild-lopsedel-hoger', array( 'class' => 'bild-lopsedel-hoger' ));
endif; ?>
I haven´t tried it yet, I will now.
No, when you remove the image size definition in your functions.php, it will no longer create extra images.
In your case you created
add_image_size( 'bild-lopsedel', 450, 9999 );
add_image_size( 'bild-lopsedel-hoger', 220, 9999, false );
Go look in your images folder, and you should have images named as:
image.jpg
image450x123.jpg
image220x123.jpg
The reason it does this is to allow you to serve smaller images if you want to. Not everybody knows how to use photoshop or what-have-you, so WordPress kindly does it for you.
It can actually, per page, reduce your bandwidth, but it will take up more disk space. Unless your site has tonnes of images in tonnes of articles, I’d leave it, but in the end, juggling your own disk space is left to your discretion.
I suppose if you wanted to cheat, you could simply resize with CSS, that would save disk space, but cost bandwidth (i.e: be slower and cost more transfer). The multi-size solution is, in my personal opinion, the best solution. I’d go with the
if (condition) {
show this image
} else {
show that image
}
Thread Starter
yurini
(@yurini)
Yes I understand.
I used conditional statements as you suggested to solve it. I am using the Advanced Custom Field´s plugin (http://www.advancedcustomfields.com) and used if statements with it, in this case “bildplacering-lopsedel” with the values of “bild-lopsedel-fullbredd”, “bild-lopsedel-hoger” and “bild-lopsedel-vanster” equivalent to the placement (fullwidth, alignright and alignleft) and added a class with the same name for the floats.
I also removed the add_image_size code and ended up with using the three sizes set under “Media” and called for “thumbnail”, “medium” etc instead. For anyone curious here´s the final code I ended up with (this code also contains some other options for getting the picturecomments etc from other custom fields to display below the picture):
[35 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]
Big thanks FMarion for taking your time!