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 need help with this code. How can I change the code so that the posts from one category to be arranged like the this scheme. scheme picture

    <?php query_posts('cat=3&amp;showposts=1'); ?>
    <?php while(have_posts()) : ?>
    <?php
    $postcount++;
    if( ($postcount % 2) == 0 ) : // skip 'even' posts
        $wp_query->next_post();
    else :
    ?>
    <?php the_post(); ?>

                        <?php if ( function_exists( 'get_the_image' ) ) { get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full','image_class' => 'alignleft', 'width' => '120', 'height' => '120' ) ); } ?>


    <?php endif; ?>
    <?php endwhile; ?>
    </div>

    <?php $postcount = 0; rewind_posts(); ?>

    <div class="even-column">
    <?php query_posts('cat=3&amp;showposts=2'); ?>
    <?php while(have_posts()) : ?>
    <?php
    $postcount++;
    if( ($postcount % 2) != 0 ) : // skip 'odd' posts
        $wp_query->next_post();
    else :
    ?>
    <?php the_post(); ?>

                        <?php if ( function_exists( 'get_the_image' ) ) { get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '60', 'height' => '60' ) ); } ?>

    <?php endif; ?>
    <?php endwhile; ?>
    </div> 
share|improve this question

2 Answers 2

up vote 0 down vote accepted

You should not use query_posts more than one time. It will create errors. You have to use wp_query.

Here is what it says in wordpress codex:

query_posts( is meant for altering the main loop. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result.

http://codex.wordpress.org/Function_Reference/query_posts

<?php $query_one = new WP_Query('cat=3&amp;showposts=1'); ?>
<?php while($query_one->have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) == 0 ) : // skip 'even' posts
    $wp_query->next_post();
else :
?>
<?php $query_one->the_post(); ?>

                    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full','image_class' => 'alignleft', 'width' => '120', 'height' => '120' ) ); } ?>


<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata();  // Important to reset it when done ?> 
</div>

<?php $postcount = 0; rewind_posts(); ?>

<div class="even-column">
<?php $query_two = new WP_Query('cat=3&amp;showposts=2'); ?>
<?php while($query_two->have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) != 0 ) : // skip 'odd' posts
    $wp_query->next_post();
else :
?>
<?php $query_two->the_post(); ?>

                    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '60', 'height' => '60' ) ); } ?>

<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // After every wp_query ?> 
</div> 

You can read more about it here:
http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
Thnak you for the answer jocken. I read the article and it helped me a lot. Can I ask you a last question about my code? –  Zlatomir Jan 5 '12 at 13:37
    
Of course you can! –  jocken Jan 5 '12 at 16:30

Don't repeat yourself ;)

I think this should do it:

<?php
    query_posts( 'cat=3' );
    $postcount = 0;
    while( have_posts() ) :
        the_post();
        $image = false;

        if ( function_exists( 'get_the_image' ) ) :
            $image = get_the_image( array(
                'custom_key' => array(
                'post_thumbnail' ),
                'default_size' => 'full',
                'image_class' => 'alignleft',
                'width' => '120',
                'height' => '120',
                'echo' => false
            ) );
        endif;

        if( ($postcount % 2) == 0 )
            $images['even'][] = $image;
        else
            $images['odd'][] = $image;

        $postcount++;
    endwhile;

foreach ($images as $colname => $colimages )
{
    echo '<div class="' . $colname . '-column">';
    foreach ( $colimages as $img )
    {
        if ( $img != false )
            echo $img;
    }
    echo '</div>';
}
share|improve this answer
    
Thanks for the answer Peter! But i have two different sizes on the thumbnails. –  Zlatomir Jan 5 '12 at 12:56

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.