Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a post thumbnail set for standard posts (when an image is featured) but for image format posts I'd like to have a separate, dynamically resizing (i.e. image resizing in the functions php via add_image_size), thumbnail. I found this snippet from the codex:

if ( has_post_format( 'video' )) {
echo 'this is the video format';
}

I've tried using it on my index.php with 0 success. Am I on the right track or should I be doing something completely different?

share|improve this question
    
What do you mean by, "dynamically resizing, thumbnail"? The intermediate image sizes are static. –  Chip Bennett Mar 9 '12 at 0:59
    
This is all I meant: add_image_size( 'index-thumb', 640, 250, true ); add_image_size( 'image-format', 630, 9999, true ); –  Colton Allen Mar 9 '12 at 1:00
    
So is your issue that you don't see that text echoing on the index.php page? or are you lacking the code to display your custom image size? –  Michelle Mar 9 '12 at 1:05
    
I tried echoing php has_post_thumbnail blah blah and it ended up completely rearranging the index page's structure in really weird ways so I just presumed it wouldn't work that way. –  Colton Allen Mar 9 '12 at 1:18

2 Answers 2

up vote 0 down vote accepted

From your comment:

This is all I meant: add_image_size( 'index-thumb', 640, 250, true ); add_image_size( 'image-format', 630, 9999, true );

So, let's assume you create custom image sizes for gallery and video (as well as a "default" size, which we'll call standard), perhaps like so:

<?php
add_image_size( 'index-standard', 640, 250, true );
add_image_size( 'image-gallery', 630, 9999, true );
add_image_size( 'image-video', 700, 9999, true );
?>

Now, in your template, where you want to output the custom image size, let's figure out which one to use, via get_post_format(), like so:

<?php
// Determine post format
$post_format = ( get_post_format() ? get_post_format() : 'standard' );
// Set image size based on post format
$thumbnail_size = 'image-' . $post_format;
// Output post thumbnail
the_post_thumbnail( $thumbnail_size );
?>

That should be it.

Note: By using get_post_format(), all you have to do is register the image size for each supported format, and you're done. You can use has_post_format(), but you'd have to add explicit code for each supported format, like so:

<?php
$thumbnail_size = 'image-standard';
if ( has_post_format( 'gallery' ) ) {
    $thumbnail_size = 'image-gallery';   
} else if ( has_post_format( 'video' ) ) {
    $thumbnail_size = 'image-video';   
}
the_post_thumbnail( $thumbnail_size );
?>

You could also use a switch instead of an if/else; either way, it's more efficient to use the get_post_format() method, above.

share|improve this answer
    
I'm trying out both of your methods right now. Quick question though. What exactly is going on here -> $post_format = ( get_post_format() ? get_post_format() : 'standard' ); ... Specifically the ... get_post_format() ? get_post_format() –  Colton Allen Mar 9 '12 at 2:35
    
I got it to work. Thanks for your help! Still curious about the "?" between the two get_post_format's –  Colton Allen Mar 9 '12 at 2:45
    
@pope: it's called Ternary Operation, it's essentially a shorthand way of writing an if/then/else. In this case, IF there's a post format (?) THEN return the format to the variable, otherwise (ELSE :) return the string 'standard'. So the syntax is basically (condition) ? true : else –  Evan Mattson Mar 9 '12 at 5:17
    
@EvanMattson nailed it. :) Here are the PHP.net docs for the ternary operator. –  Chip Bennett Mar 9 '12 at 13:49
    
@EvanMattson Thanks so much to the both of you. I learned a lot! –  Colton Allen Mar 9 '12 at 18:23

Your comments on the question lead me to believe that you are having trouble outputting the content correctly. You said you're echoing the output; try to return it instead:

if ( has_post_format( 'video' )) {
  $content = 'this is the video format';
}
return $content;

This may work if your content from this script is being output "before" the main post output, but instead you want it "inside" the main post output.

See append_content help for further discussion which might be helpful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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