Hi,
I have two columns and I want to output even posts in the first one and odd posts in the second, but I have no idea how to do so.
My current code looks like this:
<div class="row max-width">
<?php
$args = array(
'post_type' => 'sites'
);
$the_query = new WP_Query( $args );
?>
<div class="large-6 columns end">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>" class="<?php the_field('classes'); ?>">
<img src="<?php the_field('thumb'); ?>" />
</a>
<?php endwhile; endif; ?>
</div>
<div class="large-6 columns end">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>" class="<?php the_field('classes'); ?>">
<img src="<?php the_field('thumb'); ?>" />
</a>
<?php endwhile; endif; ?>
</div>
</div>
And this one outputs all posts in both columns (of course).
Can anyone help me with this?
Have yet to try this out.
but i know all posts have id.
I think you would need to write a function where if divided by 2 the modulus operator would be equal/return 0 or 1.
if the remainder is 0 or 1 it would be even. if the remainder was anything else it would be odd.
so you would need a variable for the posts when you click on “get shortlink” you’ll see the numerical identifier for it. then you’d need a bit of code (see attached link)
I have not used it in a while but the below link w/a better explanation & an example
http://snook.ca/archives/php/the_modulo_oper
Am sure better coders here can come up w/a sample.
It seems to me that this would be a good way to approach this problem
good luck
D
I’d have to say as posts are chronological or category/theme organized I don’t quite see why you’d want odd & evens.
Doesn’t’ seem like an helpfull way to sort them.
D
in the loop use the modulus operator , roughly something like this (you only need one loop)
<?php
$args = array(
'post_type' => 'sites'
);
$the_query = new WP_Query($args);
?>
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$even = false;
if (the_ID() % 2 == 0) { /* use modulus operator here to determine if id divided by 2 is a whole numer */
$even = true;
}
?>
<div class="large-6 columns end">
<?php if ($even) { ?>
<a href="<?php echo get_permalink(); ?>" class="<?php the_field('classes'); ?>">
<img src="<?php the_field('thumb'); ?>" />
</a>
<?php } ?>
</div>
<div class="large-6 columns end">
<?php if (!$even) { ?>
<a href="<?php echo get_permalink(); ?>" class="<?php the_field('classes'); ?>">
<img src="<?php the_field('thumb'); ?>" />
</a>
<?php } ?>
</div>
<?php
endwhile;
endif;
?>
& there you go, a better coder to the rescue!
That looks like it should work louie171
D