使用循环检索 WordPress 帖子

发布于 2024-10-06 07:32:04 字数 2643 浏览 2 评论 0原文

我需要实现一个滑块,它将在每张幻灯片中显示特定类别的 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)' ? 我应该如何实际实施呢?

I need to implement a slider which will show 4 post thumbnails from a particular category in each slide. For this I wrote this:

<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();

                            } ?>

The structure I need for the jquery slider is something like this:

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

But code is not working for some reason! Looks like after generating few lists the code execution does not stop and browser hangs. Have I used the function in a wrong way: 'query_posts('cat=14&posts_per_page=4&offset='.$pStart)' ?
How should I actually implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2024-10-13 07:32:04

如果您有很多帖子,则外部 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;

If you've got lots of posts, that outer while loop is going to keep on going until it's queried every post, 2 at a time!

Seems to me like you're making things complicated for yourself, so here's what I'd do instead;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文