0

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?

4
  • What do you mean by, "dynamically resizing, thumbnail"? The intermediate image sizes are static. Commented Mar 9, 2012 at 0:59
  • This is all I meant: add_image_size( 'index-thumb', 640, 250, true ); add_image_size( 'image-format', 630, 9999, true ); Commented Mar 9, 2012 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
    Commented Mar 9, 2012 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. Commented Mar 9, 2012 at 1:18

2 Answers 2

0

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.

5
  • 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() Commented Mar 9, 2012 at 2:35
  • I got it to work. Thanks for your help! Still curious about the "?" between the two get_post_format's Commented Mar 9, 2012 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 Commented Mar 9, 2012 at 5:17
  • @EvanMattson nailed it. :) Here are the PHP.net docs for the ternary operator. Commented Mar 9, 2012 at 13:49
  • @EvanMattson Thanks so much to the both of you. I learned a lot! Commented Mar 9, 2012 at 18:23
0

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.

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.