Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Robin Sturgeon
Robin Sturgeon
3,186 Points

Sizing thumbnails with custom post types

I have a custom post type page that uses a lightbox. I would like all of the images the client uploads to be displayed a certain thumbnail size on the page. Is there a way to do this? I tried with CSS but it does not seem to be working.


            <a href="<?php the_field( 'photo' ); ?>" class="photos" rel="lightbox" title="<?php the_field( 'photo_description' ); ?>" >
            <img src="<?php the_field( 'photo' ); ?>" ></a>

Thanks Robin

9 Answers

Darren Walker
Darren Walker
4,877 Points

All your images are coming a custom post type (a featured image) ?

Check into http://codex.wordpress.org/Function_Reference/add_image_size

You can set as many image sizes as you want by adding them to your functions.php file. What size do you need?

Robin Sturgeon
Robin Sturgeon
3,186 Points

I set it up so that they can just enter an image through a custom post type and it will sit in the proper box on the page. But some of the images are displaying larger, I think because they are vertical. I need them to be about 222x148. I kind of understand the add_image_size but I think I am missing something.

add_theme_support( 'post-thumbnails' );

if ( function_exists( 'add_image_size' ) ) { add_image_size( 'photos', 222, 148, true ); }

this goes in functions.php correct?

Darren Walker
Darren Walker
4,877 Points

Yes you put that in your functions PHP file, you can essentially add as many sizes as you would like. The "true" should hard crop to the image size you've mentioned.

How you display the featured image will depend on what image upload you're using. If using the default wordpress featured image box you should be able to display it with

if ( has_post_thumbnail() ) { 
        the_post_thumbnail( 'your-custom-size' ); 
    }

If you're using Advanced Custom Fields for the image upload you're going to want to use something a little different.

First define the sizes we talked about

/**
 * Add automatic image sizes
 */
if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'photos', 228, 148, true ); //(cropped)
}

Then something like this should get you started

$attachment_id = get_field('your-custom-field-id');
    $size = "photos"; // (thumbnail, medium, large, full or custom size photos we mentioned above)
    $image = wp_get_attachment_image_src( $attachment_id, $size );

To display it finally

    <img alt="Image of <?php the_title(); ?>" src="<?php echo $image[0]; ?>" />

I haven't tested it myself (at work) but theoretically something like this should display the image in a custom meta box.

Try it out and see if it works

Robin Sturgeon
Robin Sturgeon
3,186 Points

Ok, I'm still learning PHP but where does that second part of code go? And then when I display it will I still need to use "the_field( )" or is that what the "$attachment_id" does?

Darren Walker
Darren Walker
4,877 Points

That second part (and third for that matter) of the code will go into the page you're wanting to display the image on and yup, the attachment_id is essentially the same idea. It uses the Advanced Custom Fields function "get_field" to do the same thing, we're just storing it in a PHP variable called attachment_id to use with the Wordpress Function wp_get_attachment_image_src.

Robin Sturgeon
Robin Sturgeon
3,186 Points

I have it coded as this:

''' <div class="large-12 columns">

    <?php if ( have_posts() ) : 

        $attachment_id = get_field('photo');
        $size = "photos"; // (thumbnail, medium, large, full or custom size photos)
        $image = wp_get_attachment_image_src( $attachment_id, $size );
    ?>

            <a href="<?php the_field( 'photo' ); ?>" class="photos" rel="lightbox" title="<?php the_field( 'photo_description' ); ?>" >
            <img alt="Image of <?php the_title(); ?>" src="<?php echo $image['photo']; ?>" /></a>

    <?php endif; ?>

</div>

''' But it does not show the image on the page, only in the lightbox.

Darren Walker
Darren Walker
4,877 Points

Is this a live or local site? Just guessing but I assume it's the src of the image. What displays when you just echo $image on it's own line?

Robin Sturgeon
Robin Sturgeon
3,186 Points

Right now it is local. Nothing shows up.

Darren Walker
Darren Walker
4,877 Points

Try using a var_dump($image)

Robin Sturgeon
Robin Sturgeon
3,186 Points

That gives me a broken image icon.

Darren Walker
Darren Walker
4,877 Points

Robin,

Sorry for all the confusion, I just took a more in depth look at the ACF plugin as I've never used it before and see a much easier solution.

In the options section where you've added the image to show up in a metabox make sure "Return Value"of "photos" is set to image URL, then you can simply call it with

<img src="<?php the_field('photos'); ?>" />

That was working on my local server after I took some more time to look at it.

Robin Sturgeon
Robin Sturgeon
3,186 Points

I tried that too, it displays the image but is still does not resize it. I must have something wrong in the php somewhere.

Darren Walker
Darren Walker
4,877 Points

Amazing what sleep can do!

After looking at it again this morning, the original solution works, I just tried it but forget that you'll need to regenerate your images. Download and install the plugin "Regenerate Thumbnails" and run it. It should then resize to the custom image size you created and want.

Robin Sturgeon
Robin Sturgeon
3,186 Points

Ok. I have the ''' /**

  • Add automatic image sizes */ if ( function_exists( 'add_image_size' ) ) { add_image_size( 'photos', 228, 148, true ); //(cropped) }''' in my functions.php And '''<?php if ( have_posts() ) :

    $attachment_id = get_field('photo');
    $size = "photos"; // (thumbnail, medium, large, full or custom size photos)
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    

    ?>

        <a href="<?php the_field( 'photo' ); ?>" class="photos" rel="lightbox" title="<?php the_field( 'photo_description' ); ?>" >
        <img alt="Image of <?php the_title(); ?>" src="<?php echo $image['photo']; ?>" /></a>
    

    <?php endif; ?> ''' on the page with my images. The image still does not show up. Am I missing something?