0

in my homepage I have content boxes with different layouts. Every box has an image and the post title corresponding to a single post. How should I use only one single get_posts() and format the layout of the boxes differently?

Example:

H1
Post-title 1
Image 1 BIG

H2
Post-title 2
Image 2 MEDIUM

H3
Post-title 3
Image 3 SMALL
2
  • what's the problem in this? You use $posts = get_posts(); echo $posts[0]['post_title']; echo $posts[1]['post_title']; echo $posts[2]['post_title']; Commented Oct 17, 2012 at 18:14
  • I am also not sure what the problem/question is?
    – s_ha_dum
    Commented Oct 17, 2012 at 19:18

1 Answer 1

0

As @MridulAggarwal already stated, it's a pretty basic PHP task that you're confronted with:

$wpse69584_posts = get_posts( array( /* Your Arguments */ ) );

echo $post[0]['post_title'];
echo isset( $post[0]->post_excerpt ) 
    ? apply_filters( 'the_excerpt', $post[0]->post_excerpt )
    : apply_filters( 'the_content', $post[0]->post_content )
;
// etc.

Or if you're using a loop, it's even easier:

global $wpdb;
if ( have_posts() )
{
    while( have_posts() )
    {
        the_post();

        if ( 1 <= $wpdb->current_post )
        {
             $thumb_size = 'BIG';
        }
        // Add 'MEDIUM' to 2nd post and 'SMALL' to each other
        else
        {
             $thumb_size = ( 2 == $wpdb->current_post ? 'MEDIUM' : 'SMALL' );
        }

        echo '<article '.get_post_class( "post-number-{$wpdb->current_post}" ).'>';
            echo "<h{$wpdb->current_post}>";
                the_title();
            echo "</h{$wpdb->current_post}>";

            the_content();

            the_post_thumbnail( get_the_ID(), $thumb_size );
        echo '</article>';
    }
}

Note, that this won't work with a larger number as 6, as $wpdb->current_post will still increment, but there's simply no <h7> and following ;)

0

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.