PHP - 显示相同的帖子而不是下一篇文章

时间:2016-08-24 20:58:27

标签: php html wordpress

while ( have_posts()) : the_post();

我很难理解if和while语句为什么循环同一个帖子但直接在这一行之后:

COPY

我在回显标题时会显示帖子列表。

我想在这里实现的是为循环中输出的每个帖子创建不同的html布局。

干杯

2 个答案:

答案 0 :(得分:1)

我认为您正在尝试循环查询结果,如WP_Query official documentation

所示

另外,不确定为什么你会做像你这样的循环在主post循环中,所以我将它们改为if语句。

我相信你所追求的是这样的事情:

<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 7,
            'paged' => $paged );
        $wp_query = new WP_Query($args);
        $count = 0;

        if(! $wp_query->have_posts())
            die('no posts!'); // ADDED - Handle this more elegantly

        while ( $wp_query->have_posts() ) : $wp_query->the_post(); // ADDED - Use query result object?>
            <?php echo get_the_title(); ?>
            <?php
                $count++;
                if ($count === 1) { ?>
                    <div class="column small-12 margin-bottom-small">
                        <div class="panel-brown">
                            <img src="" alt="" />
                            <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
                            <div class="para-white">
                                <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
                            </div>
                        </div>
                    </div>
            <?php }
                if ($count <= 2) {
                    ?>
                    <div class="column small-12 medium-6 margin-bottom-small">
                        <div class="panel-tan">
                            <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
                            <div>
                                <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
                            </div>
                        </div>
                    </div>
            <?php }
                if  ($count <= 4) { // ADDED - Be careful, this will fire as well as the if statement above it since they are both <= 2
                    ?>
                    <div class="column small-12 medium-6 margin-bottom-small">
                        <div class="panel-tan">
                            <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
                            <div>
                                <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
                            </div>
                        </div>
                    </div>
            <?php }
            ?>
        <?php endwhile; ?>

        <!-- Pagination links -->
        <?php next_posts_link( '&larr; Older posts', $wp_query ->max_num_pages); ?>
        <?php previous_posts_link( 'Newer posts &rarr;' ); ?>

答案 1 :(得分:0)

这不起作用的原因是因为我在while循环中的多个地方调用count ++意味着它从来没有在正确的数量上。我通过将它放在if和while循环之外来解决这个问题。

感谢您的努力人员