检索与循环的wordpress岗位

时间:2010-12-05 19:54:22

标签: wordpress wordpress-theming

我需要实现一个滑块,它会在每张幻灯片中显示特定类别的4个缩略图。为此我写了这个:

<ul class= "videoSlider">
                            <?php
                            $pStart = 0;
                            $flag = true;

                            while ($flag) {

                                query_posts('cat=14&posts_per_page=4&offset='.$pStart);

                                $pStart = + 4;
                            ?>


                                <li>
                                <?php
                                if (have_posts ()) {

                                    while (have_posts ()) {
                                        the_post();
                                ?>
                                        <div onclick="something()">

                                    <?php echo the_post_thumbnail(array(215, 190)); ?>
                                         </div>

                                <?php
                                    }
                                } else {
                                    $flag = false;
                                }
                                ?>
                            </li>


                            <?php

                            wp_reset_query();

                            } ?>

jquery滑块所需的结构是这样的:

                <ui>
                      <li>
                        <div>
                            thumb 1
                        </div>
                        <div>
                           thumb 2
                        </div>
                        <div>
                            thumb 3
                        </div>
                        <div>
                            thumb 4
                        </div>
                    </li>


                    <li>
                        <div>
                            thumb 5
                        </div>
                        <div>
                           thumb 6
                        </div>
                        <div>
                            thumb 7
                        </div>
                        <div>
                            thumb 8
                        </div>
                    </li>

                </ul>

但是代码由于某种原因不起作用!在生成几个列表后,代码执行不会停止并且浏览器挂起。我是否以错误的方式使用了该功能:“query_posts('cat=14&posts_per_page=4&offset='.$pStart)”? 我该如何实际实现呢?

1 个答案:

答案 0 :(得分:0)

如果您有很多帖子,那么外部while循环将一直持续到每个帖子查询一次,每次2个!

对我而言,就像你让事情变得复杂一样,所以这就是我要做的事情;

global $wp_query;
query_posts('cat=14');

if ( have_posts() ):

    $last_post = $wp_query->post_count - 1; // index for the last post
    $counter = 0;

    echo '<ul class= "videoSlider">';

        while ( have_posts() ):

            the_post();

            if ($counter === 0)
                echo '<li>';

            echo '<div onclick="something()">';
            the_post_thumbnail(array(215, 190));
            echo '</div>';

            if ($counter === 3 || $last_post == $wp_query->current_post) {
                $counter = 0;
                echo '</li>'; // close the tag every 4th item, or if we're at the end of the loop

            } else {
                $counter++;
            }


        endwhile;

    echo '</ul>';

endif;