0

I'd like to create a new image size and write another function that basically says that if I'm applying this new image to any posts to use this new image size.

function add_image_size( 'story_thumb', 640, 640, array( 'center', 'center' ) );

Is the function I have for the new image size. I want it to be 640x640 and crop in the dead center horizontally and vertically.

Now, if I can make this apply only to posts that be great, but I'm not sure how to write this conditional statement. I'm also wondering if it's possible to take it a step further to target posts tagged with a specific parent category.

I feel like this could be modified to make this work:

if ( has_post_thumbnail() ) { 
    the_post_thumbnail( 'story_thumb' ); 
}

1 Answer 1

1

You can do something like this if you want it to apply to just single posts with post-type as post:

if ( has_post_thumbnail() ) {
    if ( is_singular('post') ) {
        the_post_thumbnail( 'story_thumb' );
    } else {
        the_post_thumbnail( );
    }
}

or this if you want to check if the post is in a particular category.

if ( has_post_thumbnail() ) {
    if ( is_singular('post') && in_category( 'category_slug' ) ) {
        the_post_thumbnail( 'story_thumb' );
    } else {
        the_post_thumbnail( );
    }
}

Where you replace category_slug with your category's slug or an array of category slugs.

To use this in a function, you can define the custom function in your functions.php file as so

if ( !function_exists('my_custom_post_thumbnail') ) {
    function my_custom_post_thumbnail() {
        if ( has_post_thumbnail() ) {
            if ( is_singular('post') ) {
                the_post_thumbnail( 'story_thumb' );
            } else {
            the_post_thumbnail( );
            }
        }
    }
}

Then replace the_post_thumbnail in your theme with my_custom_post_thumbnail

6
  • can this be used in a function? Dec 17, 2016 at 5:09
  • @DarrenBachan I've included a custom function wrapper for the code
    – Tunji
    Dec 18, 2016 at 11:14
  • Do I need an add action or anything with this function? Dec 19, 2016 at 15:56
  • this function kills the page, 500 error. Dec 22, 2016 at 16:42
  • What error are you getting in your error log
    – Tunji
    Dec 22, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.