Support » Developing with WordPress » Creating custom image sizes using add_image_size()

  • Resolved Evyatar Tal

    (@evyatartal)


    Hi,
    Iv’e added some image sizes to my website using add_image_size(), and I need to use them within widjets, but I don’t understand how.
    For example: I have a widjet on my home page wich displays recent posts, and I want to set the post image that is shown for one of the sizes I set in the functions.php.
    Is there a way to define the image-sizes using CSS?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Documentation for add_image_size()

    The string parameter that you pass to add_image_size() is the name or specific identifier for that specific image size that you define in the function.
    To display the post image size you need to pass the name or specific identifier to the_post_thumbnail() $size parameter.

    Documentation for the_post_thumbnail()

    Example:
    In function.php:
    add_image_size( “my_image_size”, $width, $height, false );

    In your Recent post:
    the_post_thumbnail( “my_image_size” );

    Hope this helps!

    Hey @shashwatmittal, thank you so much for the fast answer.
    There’s two things. First, I’m using widjets to display the post-lists, and not raw PHP as shown in your example, so I didn’t quite understand how to determine the images size using add_image_size() and widjets. Second, I have 3 locations where posts and posts images are shown, each with it’s own size, so I can’t change the defaulf value for the posts in wordpress/the widjet, because I have 3 different sizes.
    I’ll be glad if you could help me figure it out.
    Thank you!

    Moderator bcworkz

    (@bcworkz)

    Widgets are comprised of PHP code internally, so Shashwat’s suggestions are still applicable. Widget PHP code is passed data that includes the current widget area for which it’s being called, so conditionally specifying certain sizes based on location is possible. You should not alter widget PHP code unless it’s a widget you created yourself. What you can do is copy the widget code you wish to modify, giving it a new name. Then remove the original widget from each area and add in your version.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating custom image sizes using add_image_size()’ is closed to new replies.