Related Posts loop – offset

Do you mean you want to display three blocks of four related posts, and flip between the three blocks using scrollable?

If so, does the below help (not tested)? Assuming scrollable needs its content in separate divs, you could do something like this. It should produce a containing div with up to three divs inside, each containing 4 posts… assuming you have 12 posts to display. if not it should die gracefully without leaving any divs hanging open.

<?php
 $args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>12, // get all the posts at once, then split them up afterwards.
'ignore_sticky_posts' => 1
 );

$posts_per_block = 4;

$my_query = new wp_query($args); ?>

<div id="scroller-wrapper">

<?php
if( $my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post();
if ( ($my_query->current_post) == 0 ) {
    echo '<div class="scroller-block">';
}
    else if ( ( $my_query->current_post ) % $posts_per_block == 0 ) {
    echo '</div><div class="scroller-block">'; //close and open a new div every     nth post
}
the_title();
the_content(); //any other post stuff here

if ( ( $my_query->current_post + 1) == ( $my_query->post_count ) ) {
    echo '</div>'; //close the div if you run out of posts
}
endwhile; endif;
?>

</div>

NB You’re using some deprecated code in your example, eg ‘caller_get_posts’ and wp_start() – you should use ‘ignore_sticky_posts’ and the_post() respectively.

EDIT: moved the closing of the internal divs to after, not before the post content. oops

Leave a Comment