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 just found a clever piece of code on line which allows Wordpress posts to be displayed in side by side columns and works great with the title and post excerpt that I want to display.

However I would like also to display a specific featured image that I defined in the functions file in each post. Here is the code that works fine apart from a lack of output for the featured image. I'm guessing my syntax is incorrect at that point.

                <?php
                    // Custom loop that adds a clearing class to the first item in each row
                    $args = array('numberposts' => -1, 'order' => 'ASC', 'post_type' => 'treatments' ); //For this example I am setting the arguments to return all posts in reverse chronological order. Odds are this will be a different query for your project
                    $allposts = get_posts($args);
                    $numCol = 2; // Set number of columns

                    // Start Counter
                    $counter = 0;
                    foreach ($allposts as $post) {
                        $content = '<div class="six columns'.' '.($counter % $numCol == 0 ? ' alpha' : 'omega').'">'; // Add class to the columns depending on if it is odd or even
                        $content .= '<section class="treatments lightgrey-background">';
                        $content .= '<figure>';
                        $content .= '<img src="'.($post->the_post_thumbnail->small).'" />';
                        $content .= '</figure>';
                        $content .= '<h4>'.$post->post_title.'</h4>';
                        $content .= $post->post_excerpt;
                        $content .= '</section>';
                        $content .= '</div>';
                        echo $content;
                        $counter ++;
                    }
                ?>

I'd like a fallback image if the user hasnt specified a featured image too. I'd guess an if else statement similar to the short hand used to add a class to the columns would be appropriate? Here is the featured image code that is in the functions file.

<?php
// add featured image
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 120, 120, true );
add_image_size( 'small', 120, 120, true );
add_image_size( 'medium', 330, 330, true );
add_image_size( 'front_page', 460, 350, true);
add_image_size( 'header', 660, 200, true);
add_image_size( 'large', 600, 390, true );
?>
share|improve this question

1 Answer 1

This is the syntax problem:

$content .= '<img src="'.($post->the_post_thumbnail->small).'" />';

The the_post_thumbnail() function is not part of the $post object, and would print, rather than return, the post thumbnail, even if that method would work.

Try using get_the_post_thumbnail() instead:

$thumbnail = get_the_post_thumbnail( $post->ID, 'small' );

Note that this function returns the fully formed <img> tag markup, so you would need to adapt your code accordingly:

$content .= get_the_post_thumbnail( $post->ID, 'small' );

Edit

Note that 'small' is an image size already defined by WordPress, so you'll want to use a different name. I would use something location-descriptive, such as 'post-two-column':

add_image_size( 'post-two-column', 120, 120, true );

Then, reference accordingly in your loop:

$content .= get_the_post_thumbnail( $post->ID, 'post-two-column' );
share|improve this answer
    
Perfect that is just what I needed thankyou! –  Jonathan Beech Mar 13 '13 at 18:27

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.