基于标签的“相关帖子”的循环偏移量

时间:2011-09-29 作者:japanworm

当我使用jQuery工具时;“可滚动”;“我的相关帖子”部分(参见示例here), 我想知道如何实现偏移,以便在第一个循环中显示前4个帖子,然后显示接下来的4个相关帖子等。我现在正在考虑创建3个这样的循环,显示最新的12个相关帖子。

我当前的设置如下:(已更新!)

           <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">


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


 <?php
    while ( $my_query->have_posts() ) : $my_query->the_post();

        the_title();

        // 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
        if( $my_query->current_post != ( $my_query->post_count - 1 ) && ( ( $my_query->current_post + 1 ) % 4 ) == 0 ):
            ?>
            </div>

<div>
  <?php
        endif; 

 endwhile;
    ?>

            </div>



</div></div>

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


<br clear="all" />
然而,我只是不知道如何在现有代码中实现它。不确定这是否可行,或者我是否必须首先使用不同的方法来创建循环以使其工作?

提前非常感谢!:)

EDIT: Last updated on October 16th. I still haven\'t figured this out, please help!

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

执行三个单独的查询是不必要的,效率也很低,对所有12篇文章执行一个查询,并每四篇文章输出一个容器标记。

$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="container">
    <?php
    while ( $my_query->have_posts() ) : $my_query->the_post();

        the_title();

        // 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
        if( $my_query->current_post != ( $my_query->post_count - 1 ) && ( ( $my_query->current_post + 1 ) % 4 ) == 0 ):
            ?>
            </div>
            <div class="container">
            <?php
        endif;

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

结束

相关推荐

_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