I bought a wordpress template and try to modify the codes in the brand page to display 3 columns. below is the scenario:
Currently Display:
a | b | c | d
e | f | g | h
i | j | k
I want the display to Becomes
a | d | g
b | e | h
c | f | i
The codes below display the current one. Can anyone help me to correct the codes below to display the above scenario.
CODES:
<?php
$args = array(
'post_type' => 'partner',
'posts_per_page' => '-1',
'order' => 'ASC',
'post__not_in' => array( $post->ID )
);
// the query
$query = new WP_Query( $args );
// The Loop
?>
<section class="recent-posts clear">
<?php
$lastChar = '';
$count_posts = wp_count_posts('partner')->publish;
$i = 0;
?>
<?php if ($query->have_posts()) : while($query->have_posts()) : $i++;
if(($i % 3) == 0) : else : $query->the_post(); ?>
<?php
global $post;
$brandname = $post->post_title;
$char = $brandname[0];
?>
<div style="float:left; width:24%; margin-right:10px;">
<?php
if ($char !== $lastChar) {
if ($lastChar !== '')
echo '<br>';
echo "<div style='padding:10px; background:red;
color:white; font-weight:bold;'>" .strtoupper($char)."</div>";
//print A / B / C etc
$lastChar = $char;
}
echo $brandname;
?>
<?php the_content(); ?>
</div>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif; ?>
</section>
Try this code
Add this CSS
Here you go. You just need a little counting and an edit in your loop. See comments in code -
Updated - changed the syntax a bit and moved the counter to the end of the while loop.
In your code try to remove
and replace it with
Addition to achieve the result in 3 columns in a group vertically is the masonry css technique. Here is my final result:
CSS