Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Gonzaloº Caminos
Gonzaloº Caminos
2,160 Points

array $size not working in wp_get_attachment_image_src function

Hi. I am adapting the code from Bootstap to WordPress i want use array $size in function wp_get_attachment_image_src but this function only works with "thumbnail" and not with my values.

    <?php
            $thumbnail_id = get_post_thumbnail_id(); 
            $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, array('250', '300'), true );
      ?>

Don't work.

    <?php
            $thumbnail_id = get_post_thumbnail_id(); 
            $size = array('width'=>250,'heith'=>250);
            $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, $size, true );
      ?>

Don't work.

Gonzaloº Caminos
Gonzaloº Caminos
2,160 Points

Im triyng to make this modification because i need images with the same size and not this:

http://imgur.com/a/GmDbF

2 Answers

Muhammad Ehsan Hanif
Muhammad Ehsan Hanif
8,235 Points

You can use the add image size function in your functions.php file like this.

add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
    add_image_size( 'category-thumb', 300 ); // 300 pixels wide (and unlimited height)
    add_image_size( 'homepage-thumb', 250, 250, true ); // (cropped)
}

and then use it in theme file with the code below.

wp_get_attachment_image_src( $thumbnail_id, 'category-thumb' );

Reference: https://developer.wordpress.org/reference/functions/add_image_size/