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've been working on this for many, many, many weeks already. I've already asked similar questions here and elsewhere, but nobody seems to be able to sovle this problem. Maybe it's not possible anyways?

My related posts are tag based and the code looks like this:

<?php 
                        $backup = $post;
                        $tags = wp_get_post_tags($post->ID);
                        if ($tags) {
                            $tag_ids = array();
                            foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

                            $args=array(
                                'tag__in' => $tag_ids,
                                'post__not_in' => array($post->ID),
                                'showposts'=>4, // Number of related posts that will be shown.
                                'caller_get_posts'=>1
                            );
                            $my_query = new wp_query($args);
                            if( $my_query->have_posts() ) {
                                while ($my_query->have_posts()) {
                                    $my_query->the_post();
                                ?>

I want users to be able to scroll to the next 4 related posts after that, and again to the next 4. A total of 12 posts. I'm using jQuery Tools' Scrollable to do so.

Each loop will sit between divs so that scrollable will where to cut the content and where to scroll.

All I could find about offsetting your loop was this:

   // FIRST LOOP: display posts 1 thru 4
<?php query_posts('showposts=4'); ?>
<?php $posts = get_posts('numberposts=4&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "4") { break; } else { ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php $count1++; } ?>
<?php endforeach; ?>


// SECOND LOOP: display posts 5 thru 8
<?php query_posts('showposts=5'); ?>
<?php $posts = get_posts('numberposts=4&offset=4'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "4") { break; } else { ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php $count2++; } ?>
<?php endforeach; ?>


// THIRD LOOP: display posts 9 thru 12
<?php query_posts('showposts=4'); ?>
<?php $posts = get_posts('numberposts=4&offset=8'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "4") { break; } else { ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php $count3++; } ?>
<?php endforeach; ?>

I tried, but keep getting error messages. Basically I just have no clue how to combine my original code with that one.

Or is there a more elegant solution?

EDIT: UPDATE October 17th

The code basically works, there's only ONE HUGE PROBLEM. It now produces the same related posts in every single entry, even in posts that don't even have any tags!! Also, it messes up the comments, too! It doesn't display the correct number of comments and shows the pingbacks of another post instead. Any idea why?

<h2>Related Posts</h2>                      

<!-- "previous page" action -->
<a class="prev browse left"></a>

<?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); ?>

<!-- root element for scrollable -->
<div class="scrollable" id=chained>   

    <!-- root element for the items -->
    <div class="items">

<?php
if( $my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post();
if ( ($my_query->current_post) == 0 ) {
    echo '<div>';
}
elseif ( ( $my_query->current_post ) % $posts_per_block == 0 ) {
    echo '</div><div>'; //close and open a new div every nth post
}
elseif ( ( $my_query->current_post + 1) == ( $my_query->post_count ) ) {
    echo '</div>'; //close the div if you run out of posts
} ?>

<div class="relatedPosts"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(120,80)); ?></a>
<div class="relatedPosts_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div></div>


<?php
endwhile; endif;
?>

    </div>

</div>

<!-- "next page" action -->
<a class="next browse right"></a>

I really hope that finally somebody can help me figure this out.

Thanks a lot in advance.

share|improve this question
1  
Please include actual error messages you are getting. Also query_posts() should not be used for creating multiple loops - I'd clean that up because you are unlikely to get answers about complicated issues with code that is not using loops properly. –  Rarst Oct 16 '11 at 21:14
    
That's what I thought anyways, so I tried to avoid using "query_posts" until now. Thanks, it seems that we're getting closer to solve the problem (see my updated question). Thanks! :) –  japanworm Oct 17 '11 at 2:50
add comment

2 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
Yes, yes! You got it :) Your code seems to work. Awesome! There's only one little issue. I need to replace "the_title" and "the_content" with what I want to be displayed, but I have this within php tags and so now I the syntax error message: syntax error, unexpected '<'. I know what I have to do, but I'm not sure how to implement it. I updated my question, could you please have a quick look at the code? Thanks so much!! :) –  japanworm Oct 17 '11 at 2:45
    
Good! When mixing up HTML with php, you need to close the <?php ?> tag first. eg <?php the_title(); ?><p>This is plain HTML<?php echo " with some PHP"; ?></p><?php the_content(); ?>. Does that make sense? Alternatively you could stay in PHP and echo out the HTML like so: <?php echo "<p>I am inside some PHP tags</p>"; ?> –  djb Oct 17 '11 at 9:45
    
Awesome, I got it to work. Thanks so much! I tried to get this to work for so long, so you have NO IDEA how much you helped me out!!! :) :) –  japanworm Oct 17 '11 at 13:35
1  
It can go anywhere before the $tag_ids variable is required, and as long as it's enclosed in php tags it's ok :). Try this: pastebin link? –  djb Oct 17 '11 at 17:07
1  
thanks for the link. your site looks very nice. i think we're nearly there now. none of the code on your pasted page should generate that <div>....</div> markup, but i notice you've now got some HTML comments in the output, so it looks as though your php file's changed? would you mind re-pasting? Otherwise the markup is as expected, it's just that scrollable is cloning your elements to create a 'circular' scroll effect - i think you can remove {circular:true} from alltuts\js\scrollable.js to stop this behaviour. –  djb Oct 19 '11 at 14:43
show 12 more comments

You can simply try the code from here Show Related Post in WordPress

I think you're not ending each query by

wp_reset_query();

Hope it helps.

share|improve this answer
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.