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'm working on trying to get my homepage template to display post-format 'gallery' a little different than the other formats but having no luck so far.

Here is a snippet 'inside the loop' that shows what I am trying to accomplish. Basically saying if post-format = 'aside' do x, elseif post-format is gallery, then I want to show a thumbnail, else just content.

<div class="entry">
    <?php

    if ( has_post_format( 'aside' , $post_id )) {
        the_excerpt(__('Continue Reading &rarr;', 'wptumble-fluid'));

    } elseif ( has_post_format( 'gallery' , $post_id )) {

        if ( has_post_thumbnail() ) {
                // use the thumbnail ("featured image")
                $thumb_id = get_post_thumbnail_id();
            the_post_thumbnail( 'thumbnail' ); 
        } else {
            $attachments = get_children( array(
                'post_parent' => get_the_ID(),
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'order' => 'ASC',
                'orderby' => 'menu_order ID',
                'numberposts' => 1)
            );
            foreach ( $attachments as $thumb_id => $attachment )
                echo wp_get_attachment_image($thumb_id, 'thumbnail' ); 
            }
    } else {
            if ( $fluidtheme_options['fluid_post_content'] == "content" ) the_content(__('Continue Reading &rarr;', 'wptumble-fluid')); else the_excerpt();
    }

        wp_link_pages( $page_link_args );
    ?>

</div><!-- /.entry -->

The aside part works, as does the final option which checks the theme options page. The only thing not working is my elseif statement where I want it to grab the post attachment.

Probably just doing my php wrong, but this is the closest I can seem to come up with. Still in the learning process.

thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

This one's an easy fix! The first argument in has_post_format() is a string in the format post-format-{type}, e.g. post-format-aside, or post-format-gallery, etc.

So, e.g., change this:

has_post_format( 'aside' , $post_id )

To this:

has_post_format( 'post-format-aside' , $post_id )

Wash, rinse, and repeat for all uses of has_post_format().

EDIT

Given your example code, I would even suggest another potential option, combining get_template_part() and get_post_format().

If you create template part files entry-aside.php and entry-gallery.php, along with a default, entry.php, then:

1) Move this code to entry-aside.php (and remove unsupported parameters):

the_excerpt();

2) Move this code to entry-gallery.php:

if ( has_post_thumbnail() ) {
    // use the thumbnail ("featured image")
    $thumb_id = get_post_thumbnail_id();
    the_post_thumbnail( 'thumbnail' ); 
} else {
    $attachments = get_children( array(
        'post_parent' => get_the_ID(),
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'numberposts' => 1)
    );
    foreach ( $attachments as $thumb_id => $attachment )
        echo wp_get_attachment_image($thumb_id, 'thumbnail' ); 
}

3) Move this code to entry.php

if ( $fluidtheme_options['fluid_post_content'] == "content" ) the_content(__('Continue Reading &rarr;', 'wptumble-fluid')); else the_excerpt();

4) Replace that entire if/else conditional block with this:

get_template_part( 'entry', get_post_format() );

Now, it might be overkill for your current use case - but the advantage is that, if in the future you decide to support other post format types, you simply have to add entry-{type}.php, and your template will already be set up to support it! For example, if you add support for quotes, simply add entry-quote.php, with whatever appropriate code, and you don't have to change any existing code.

share|improve this answer
    
thank you, both solutions work great, no idea which to pick as winner, need option to select both –  shawn Jun 25 '11 at 0:42
    
I don't think either one of us will fuss one way or the other. :) Just FYI: I added to my answer another idea for you to consider. –  Chip Bennett Jun 25 '11 at 1:34
    
That is exactly what I was after. I was just searching through stack for post-format archive pages, found this 'taxonomy_post_format-post-format-gallery.php' Your solution should make this much easier. I know they are not directly related, but close enough... I do indeed plan on having separate styles for both single and archive post-formats. –  shawn Jun 25 '11 at 3:07
1  
the_excerpt() doesn't take any argument. –  kaiser Sep 20 '12 at 15:38
    
Thanks, @kaiser; edited to clarify. –  Chip Bennett Sep 20 '12 at 19:35

I like to use get_post_format for this:

if ( 'gallery' == get_post_format() ) {
    print 'TACO PARTY!!!';
}
share|improve this answer
    
thnx Michael, have 2 diff options, both work, not sure which is more efficient, has or get post format, either way I am happy –  shawn Jun 25 '11 at 0:32

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.