2 queries with counters

时间:2014-12-15 作者:Ciaran Gaffey

我在一个页面上有两个循环,都包含一个计数器,每6篇文章就实现一个略有不同的代码。出于某种原因,尽管第二个循环有时在4项而不是6项之后实现不同的代码。

是否未正确重置回路?

<ul class="tabs">
                        <li class="tab-link current" data-tab="tab-1">Most Popular</li>
                        <li class="tab-link" data-tab="tab-2">Recent</li>

                    </ul>




                    <div id="tab-1" class="tab-content current">
                    <div class="slider">
                        <div class="slide">
                        <?php
query_posts(\'meta_key=post_views_count&orderby=meta_value_num&order=DESC&post_type=atls_video&posts_per_page=-1\');
if (have_posts()) : while (have_posts()) : the_post();$count++; ?>
                            <?php if ($count%6== 0) : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>
                            </div>
                            <div class="slide">

                            <?php else : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>

                            <?php endif; ?>
                            <?php endwhile; endif; wp_reset_query();?>
                        </div>


                        <div style="clear:both;"></div>



                    </div>
                    </div>






                    <div id="tab-2" class="tab-content">
                    <div class="slider">
                        <div class="slide">
                                <?php
query_posts(\'orderby=date&order=DESC&post_type=atls_video&posts_per_page=-1\');
if (have_posts()) : while (have_posts()) : the_post();$count++; ?>
                            <?php if ($count%6== 0) : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>
                            </div>
                            <div class="slide">

                            <?php else : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>

                            <?php endif; ?>
                            <?php endwhile; endif; wp_reset_postdata();?>
                        </div>


                        <div style="clear:both;"></div>
                    </div>
                    </div>

1 个回复
SO网友:Ciaran Gaffey

在Robert和Pieter的建议下,我使用WP\\u Query而不是Query\\u posts重写了循环。现在可以了。谢谢各位。

代码如下:

    <ul class="tabs">
                        <li class="tab-link current" data-tab="tab-1">Most Popular</li>
                        <li class="tab-link" data-tab="tab-2">Recent</li>

                    </ul>




                    <div id="tab-1" class="tab-content current">
                    <div class="slider">
                        <div class="slide">
                        <?php

                        $count = 0;

                        $query = new WP_Query( 
                        array( 
                        \'meta_key\' => \'post_views_count\',
                        \'orderby\' => \'meta_value_num\',
                        \'order\' => \'DESC\',
                        \'post_type\' => \'atls_video\',
                        \'posts_per_page\' => \'-1\'
                                 ) );



while ( $query->have_posts() ) : $query->the_post();
$count++; ?>
                            <?php if ($count%6== 0) : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>
                            </div>
                            <div class="slide">

                            <?php else : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>

                            <?php endif; ?>
                            <?php endwhile; ?>
                            <?php wp_reset_postdata();?>
                        </div>


                        <div style="clear:both;"></div>



                    </div>
                    </div>






                    <div id="tab-2" class="tab-content">
                    <div class="slider">
                        <div class="slide">
                                <?php

                        $count = 0;

                        $query = new WP_Query( 
                        array( 
                        \'orderby\' => \'date\',
                        \'order\' => \'DESC\',
                        \'post_type\' => \'atls_video\',
                        \'posts_per_page\' => \'-1\'
                                 ) );



while ( $query->have_posts() ) : $query->the_post();
$count++; ?>

                            <?php if ($count%6== 0) : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>
                            </div>
                            <div class="slide">

                            <?php else : ?>
                                <div class="video-entry m-all t-1of2 d-1of3 cf">
                                    <div class="video-thumb">
                                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( \'video-thumb\' ); ?></a>
                                    </div>
                                    <div class="video-text">
                                        <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                        <a href="<?php the_permalink(); ?>" class="read-more">Read More</a>
                                    </div>
                                </div>

                            <?php endif; ?>
                            <?php endwhile; wp_reset_postdata();?>
                        </div>


                        <div style="clear:both;"></div>
                    </div>
                    </div>

结束

相关推荐

Infinite blog loop

我不明白为什么我会收到一个无限的博客帖子。当我注释内容中的循环代码时。php,它不再循环。我正在尝试根据post格式发布项目,但到目前为止失败了。如果我需要提供更多信息,请告诉我!我很感激。所容纳之物php:<?php /** * The default template for displaying content. Used for both single and index/archive/search. * * @subpackage Foundatio