• yurini

    (@yurini)


    I´m using post thumbnails for my theme. The problem is that I cannot choose from different post thumbnail sizes to output in my headlines.

    My functions.php looks like this in witch I added two sizes called “bild-lopsedel” and “bild-lopsedel-hoger”:

    add_theme_support( 'post-thumbnails' ); 
    
        add_image_size( 'bild-lopsedel', 450, 9999 );
        add_image_size( 'bild-lopsedel-hoger', 220, 9999, false );
        }  
    
        add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' );
        function custom_image_sizes_choose( $sizes ) {
            $custom_sizes = array(
                'bild-lopsedel' => 'Bild i löpsedel - fullbredd',
                'bild-lopsedel-hoger' => 'Bild i löpsedel - högerjusterad'
            );
            return array_merge( $sizes, $custom_sizes );
        }  
    
    ?>

    I would like to make an if statement that checks if the post thumbnail has the add_image_size “class” of ‘bild-lopsedel’ or ‘bild-lopsedel-hoger’ and output different sizes and classes depeding on that. Something like (code below doesnt work):

    <?php
    if ( has_post_thumbnail('bild-lopsedel'));
    the_post_thumbnail( 'bild-lopsedel', array( 'class' => 'bild-lopsedel' ));
    elseif ( has_post_thumbnail('bild-lopsedel-hoger'));
    the_post_thumbnail( 'bild-lopsedel-hoger', array( 'class' => 'bild-lopsedel-hoger' ));
    endif; ?>

    The post thumbnail in the headlines could then be shown in different sizes and be floated to the right or left depending on what add_image_size and css-class is signed to it.

    Is there a way to solve it with has_post_thumbnail or is there any other suggestions?

    Would be greatful for help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • FMarion

    (@fmarion)

    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.

    FMarion

    (@fmarion)

    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to show specific post_thumbnail?’ is closed to new replies.