循环不显示帖子

时间:2017-09-13 23:14:50

标签: php wordpress while-loop

我想在我的Loop中有两个不同的查询,它位于index.php中。我正在使用WP codex,但它不起作用。我想稍后在每个帖子中都有自己的特殊DIV,所以这只是我工作的开始。

问题是,代码的第二部分不起作用,我不明白为什么。至于我读过的手抄本,一切都应该没问题。请帮帮我。

 <div class="col1">  
        <?php
        $my_query = new WP_Query('category_A tym=featured&posts_per_page=1');
        while ($my_query->have_posts()) : $my_query->the_post();
            $do_not_duplicate = $post->ID;
            ?>
            <!-- Do stuff... -->
            <?php get_template_part('content', get_post_format()); ?>
        <?php endwhile; ?>

        <!--Over here everything works fine!-->



        <!--This code doesnt show up. It is supossed to show 1 post, only heading and date with author. But it doesnt show nothing at all.-->

        <?php
        $my_queryOne = new WP_Query('posts_per_page=1');
        while ($my_queryOne->have_posts()) : $my_queryOne->the_post();
            if ($post->ID == $do_not_duplicate)
                continue;
            ?>
            <!-- Do stuff... -->
            <h2 id="post-<?php the_ID(); ?>">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                    <?php the_title(); ?></a></h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <?php
        endwhile;
        ?>     

    </div>

2 个答案:

答案 0 :(得分:1)

最好回顾一下WordPress的编码指南。您需要确保内联PHP的每个打开和关闭标记都在它自己的行上。

点击此处查看:

https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#opening-and-closing-php-tags

答案 1 :(得分:1)

WP_Query将数组作为参数 - 即

wp_reset_postdata()

此外,在运行多个查询时 - 在第一个循环后使用device

这里有很好的例子:

https://codex.wordpress.org/Class_Reference/WP_Query

相关问题