Offset for Loop

时间:2011-10-12 作者:japanworm

我想为我的相关职位抵消4个职位。我正在使用Flowplayer的jQuery工具滚动,所以我希望人们能够滚动浏览最新的4篇帖子,然后是接下来的4篇帖子等等。

我尝试了类似的方法,但不断收到错误消息:

<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; ?>
我得到的错误如下:

分析错误:语法错误,在/hermes/bosweb25a/b155/ipg中出现意外的T\\u ENDWHILE。zoomingjapancom/wp内容/主题/所有子项/单个。php在线165

第165行:

<?php endwhile; else: ?>
EDIT: 这是我的原始设置,没有偏移量(只是相同的循环两次)。这工作正常,没有任何错误:

<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>
这是整首单曲。php:http://pastebin.com/x2mbQUFM

我似乎无法发现错误,希望有人能帮助我。提前多谢!

1 个回复
最合适的回答,由SO网友:patnz 整理而成

这是一个php错误,可能是因为它希望在endwhile之前关闭其中一个if语句。如果看不到所有代码,很难判断,但通过猜测检查,这一个已关闭:

if ($tags) {
我也会用花括号把你的头发包起来。也许只是偏好而已。。。

foreach($tags as $individual_tag){ $tag_ids[] = $individual_tag->term_id; }
只需检查代码,确保所有内容都已正确关闭。这不是第165行上的错误,而是之前的错误,但这正是php意识到某些错误并抛出错误的地方。

结束

相关推荐

_excerpt()、get_the_excerpt()和the_content()都杀死了“The Loop”

在调用\\u摘录()之前,\\u permalink()会显示正确的内容。之后,它不会。。。 <?php global $query_string; //strip out the \"pagename=blog\" so that the query will grab all of the posts instead of the content of the blog page