0

I want to have an offset of 4 posts for my related posts. I'm using Flowplayer's jQuery Tools Scrollable, so I want people to be able to scroll through the 4 latest posts, then the next 4 posts etc.

I tried something like that, but I keep getting error messages:

<h2>Related Posts</h2>                      


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

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

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


      <!-- 1-5 -->
      <div>

            <!-- Related Posts-->

                             <?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),
    'posts_per_page'=> 12
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ):
?>  
    <div class="relatedPosts">
    <?php
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <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>
// if this is not the last post
        // and remainder of current post plus one then divided by four is zero
        // close the container and open a new one
      <?php  if( $my_query->current_post != ( $my_query->post_count - 1 ) && ( ( $my_query->current_post + 1 ) % 4 ) == 0 ):
            ?>
            </div>
            <div class="relatedPosts">
            <?php
        endif;

    endwhile;
    ?>
    </div>        
<?php
endif; ?>

    <?php
    $post = $backup;
    wp_reset_query(); ?>

                            <!-- end Related Posts-->
      </div>

      <!-- 6-10 -->
      <div>

   <!-- Related Posts-->

                            <!-- end Related Posts-->
      </div>

   </div>

</div>

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


<br clear="all" />


        </div>
        <div class="postBoxMidInner">



        <?php comments_template(); ?>
            </div>
            </div>
            <div class="postBoxBottom"></div>
        </div>
        <?php endwhile; else: ?>

        <p>Sorry, but you are looking for something that isn't here.</p>

    <?php endif; ?>

The error I get is the following:

Parse error: syntax error, unexpected T_ENDWHILE in /hermes/bosweb25a/b155/ipg.zoomingjapancom/wp-content/themes/alltuts-child/single.php on line 165

Line 165:

<?php endwhile; else: ?>

EDIT: This is my original setup without the offset (just the same loop twice). This works fine without any errors:

<h2>Related Posts</h2>                      


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

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

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


      <!-- 1-5 -->
      <div>

            <!-- Related Posts-->

                            <?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();
                                    ?>
<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
                                    }
                                    echo '';
                                }
                            }
                            $post = $backup;
                            wp_reset_query();
                             ?>

                            <!-- end Related Posts-->
      </div>

      <!-- 10-15 -->
      <div>

   <!-- Related Posts-->
                            <?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();
                                    ?>
<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
                                    }
                                    echo '';
                                }
                            }
                            $post = $backup;
                            wp_reset_query();
                             ?>
                            <!-- end Related Posts-->
      </div>

Here's the whole single.php: http://pastebin.com/x2mbQUFM

I cannot seem to spot the mistake and hope somebody can help me out here. Thanks a lot in advance!

1 Answer 1

1

It's a php error probably because its expecting either one of your if statements to be closed before the endwhile. It's hard to tell without seeing all the code but at a guess check this one is closed:

if ($tags) {

also I would wrap your foreach in curly brackets. Maybe just preference though...

foreach($tags as $individual_tag){ $tag_ids[] = $individual_tag->term_id; }

Just go through the code and make sure everything is closed properly. It's not an error on line 165, it's before there but that's just where php realises somethings wrong and throws the error.

5
  • I updated my question with my original setup and the whole single.php file. I cannot seem to spot the error. Thanks!
    – japanworm
    Commented Oct 12, 2011 at 14:30
  • 1
    The if($tags) { statement isn't closed. You need to add a closing curly brace } either directly after the foreach block (in which case the query will still be made but there will be no tags for related posts) or after the entire related posts block - essentially saying if there are no tags, skip this section altogether.
    – patnz
    Commented Oct 13, 2011 at 1:40
  • Thanks. I tried VARIOUS positions for the closing curly brace, but I keep getting error messages. I know I'm asking a lot, but could you just paste it at the end of the entire related block just as you mentioned into my code above and paste the whole code into your answer. Sorry, I feel so stupid, but I just can't seem to find the right position. Thanks sooo much!!!! :)
    – japanworm
    Commented Oct 13, 2011 at 15:07
  • 1
    I'm about to be out of contact for a few days and don't have time to do it now. I'm not sure if the copy I got from pastebin had been messed up but my advise would be make sure your code is tidy and indented properly. All '{' need to be closed with a '}' , all 'if($foo):' need to be closed with an 'endif;' all 'while($foo > $bar):' need to be closed with a 'endwhile;' all 'foreach($foo as $bar):' need to be closed with a 'endforeach;' If the error message changes it may be a new problem, but the unexpected T_ENDWHILE is simply a matter of ensuring your code blocks are open and closed correctly
    – patnz
    Commented Oct 13, 2011 at 23:53
  • Thanks so much for trying so hard. I tried out a lot, but just can't seem to get it to work. I'll have to give up for now, but thanks a lot for everything! :)
    – japanworm
    Commented Oct 14, 2011 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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