1

I'm trying to post images from a custom post type into columns based on the amount of posts that are returned in the loop. This is my code:

<?php 
// args  
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'sponsor',
    'meta_key'      => 'sponsor_headline',
    'meta_value'    => 'Partner'
);
// query
$the_query = new WP_Query( $args );

?>
<?php if( $the_query->have_posts() ): ?> <h1>Partners<h1><hr>
    <?php while( $the_query->have_posts() ) : $the_query->the_post();
if($post_count=1){
echo '<div class="flex_column av_one_fifth flex_column_div"></div><div class="flex_column av_one_fifth flex_column_div"></div><div class="flex_column av_one_fifth flex_column_div"><a target="_blank" href="'.get_field('sponsor_url').'"><img src='. get_field('sponsor_logo').' alt="" /></a></div><div class="flex_column av_one_fifth flex_column_div"></div><div class="flex_column av_one_fifth flex_column_div"></div>'; 
} elseif($post_count=2) {
echo '2;
} elseif($post_count=3) {
echo '3';
} elseif($post_count=4) {
echo '4';
} else {
echo '<div class="flex_column av_one_fifth flex_column_div"><a target="_blank" href="'.get_field('sponsor_url').'"><img src='. get_field('sponsor_logo').' alt="" /></a></div>';
}?>
    <?php endwhile; ?><div class="clear"></div>
<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

I'm hoping to achieve that no matter what they will always be the same size: but depending on what the post count is they will be differently placed: This is graphically what I'm looking for (where x would be the post in the 5 column grid being created:

if ($post_count=1){
| | |x| | |
} elseif($post_count=2) {
| |x| |x| |
} elseif($post_count=3) {
|x| |x| |x|
} elseif($post_count=4) {
|x|x|x|x| |
} else {
|x|x|x|x|x|

Any ideas?

2

$post_count is a WP_Query variable. You need $the_query->post_count

if( $the_query->have_posts() ): ?> <h1>Partners<h1><hr> <?php 
  while( $the_query->have_posts() ) :
   $the_query->the_post();
    if($the_query->post_count=1){
       // ...

Otherwise it looks like it should work though I am not in a position to test this right now.

  • the problem is how do you put the different spacings in them? – rudtek Aug 31 '15 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.