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 am displaying only thumbs and title on index pages using following code;

 <div id="column1">
    <?php query_posts('cat=22&showposts=2'); ?>
<?php $posts = get_posts('cat=22&numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "2") { break; } else { ?>
<a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_post_thumbnail( 'homepage-thumb' ); ?></a>
<br><a style="color:#666;"><?php the_title(); ?></a><br>
<a style="color:#1C477E;"><?php the_field('price'); ?></a>
<div style="clear:both;"></div>
<?php $count1++; } ?>
<?php endforeach; ?>
</div>

    <div id="column2">
        <?php query_posts('cat=22&showposts=2'); ?>
<?php $posts = get_posts('cat=22&numberposts=2&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
<a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_post_thumbnail( 'homepage-thumb' ); ?></a>
<br><a style="color:#666;"><?php the_title(); ?></a><br>
<a style="color:#1C477E;"><?php the_field('price'); ?></a>
<div style="clear:both;"></div>
<?php $count2++; } ?>
<?php endforeach; ?>
    </div>

This cause problem when post in some category are less or more than described number of posts in one column. In case of less posts, other columns remain empty so looks odd. I want to show them in rows instead of columns so number of posts should not effect the category pages.

share|improve this question
    
One - you're making four post queries where one would do fine. Two - Are you only trying to get 4 posts (two columns of two, or are you trying to get all the posts available and divide them into columns? –  goldenapples Feb 23 '12 at 21:21
    
@goldenapples I am using this code at two places. One index page where limited number of posts will be shown from each category (say 4). Second at category page where all posts of that category should be displayed. but one thing main thing I want is to divide them in ROWS instead of COLUMNS because if I use columns as main variable, then number of posts (less then specified or more then specified) affect page layout. –  Ahmad Feb 24 '12 at 6:17
add comment

2 Answers

up vote 1 down vote accepted

Easiest way to control column and/or row layouts I know of is to make use of modulus. I broke it down in a tutorial and use it with wordpress in my examples.

Wordpress and PHP Modulus

The first example is basic, while the second is a bit more advanced and utilizes post images/attachments. If it helps let me know or give the page a +1 for me :-)

share|improve this answer
    
Hi Neil, Its amazing and at last I solved it with your help as I post this question on many forums but could not find solution. There is little problem as it gives latest posts. I want to put this on category page, so how can I amend it to display posts of specifically that category. –  Ahmad Feb 27 '12 at 19:31
add comment

if i understand you correctly, the following will get the posts and put them into rows, at 3 posts per row. you can adjust the rows by adjusting the $columns variable. you can also adjust the CSS to suit, it was just for me to get the posts floated and visible since i don't have a lot of thumbs to test with.

<style>
#test, .row{overflow: hidden; margin-bottom: 22px;}
.post {float: left; width: 150px; height: 150px; margin-right:20px; background: pink;}
.post.last {margin-right: 0;}
.post img {max-width: 100%;}
</style>

<div id="test">

    <?php 
    global $post;
    $tmp_post = $post;

    $columns = 3; //number of posts per row

    $args = array( 'cat'=>22,
            'numberposts' => 9 );

    $count = 1;

    $posts = get_posts($args); if($posts): ?>

    <div class="row">

        <?php 

        foreach ($posts as $post) : setup_postdata($post); 


        $class = ( $count%$columns == 0 ) ? 'last' : ''; ?>

        <div class="post">
            <a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_post_thumbnail( 'homepage-thumb' ); ?>
            <br/><?php the_title(); ?><br/>
            <?php if(function_exists('the_field')) the_field('price'); ?>
            </a>
        </div><!--.post-->

        <?php 
        if ( $count%$columns == 0 ) { ?>
            </div><!--.row-->
            <?php if ($count < count($posts)) : ?> <div class="row"> <?php endif; ?> 
        <?php }


        $count++;  
        endforeach; ?>
    </div><!--.row-->

<?php endif;
$post = $tmp_post;
?>

</div><!--#test-->
share|improve this answer
    
sorry it gives following error; Parse error: syntax error, unexpected T_VARIABLE –  Ahmad Feb 24 '12 at 12:08
    
did you even try to fix it? it gave the exact location of the error and parse errors just mean that some kind of ; , ) or something is missing or out of place. looks like i forgot a ; when adding the $columns variable to make it easier for you to modify. i've adjusted my code. –  helgatheviking Feb 24 '12 at 12:27
add comment

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.