Posts_per_page类别wordpress显示1个帖子,而不是多个

我想我的循环做错了什么,或者我的代码坏了。我想显示特定类别的所有帖子。无论我做什么,我只会看到1条帖子。


<ol>


    <?php

    $args = array(

       'category_name'=>'test-category',

       'posts_per_page' => 15,

       'nopaging' => true

    );

    $query = new WP_Query( $args );

    while ( $query->have_posts() ) : $query->the_post();

        //Post data

        echo get_the_post_thumbnail(get_the_ID());

    endwhile;

    ?>


                                            <li data-href="<?php $trink = get_the_permalink(); echo preg_replace("#/$#im", '', $trink);?>">

                        <div>

                            <a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

                        </div>

                    </li>


    </ol>

我在这里做错了什么?


慕神8447489
浏览 242回答 1
1回答

潇潇雨雨

您endwhile;的位置错误:构建<li>标记的代码不在循环中,这就是为什么您只看到一篇文章的原因。它应该是:<ol><?php$args = array(&nbsp; &nbsp; 'category_name' => 'test-category',&nbsp; &nbsp; 'posts_per_page' => 15,&nbsp; &nbsp; 'nopaging' => true);$query = new WP_Query( $args );while ( $query->have_posts() ) : $query->the_post();&nbsp; &nbsp; //Post data&nbsp; &nbsp; echo get_the_post_thumbnail(get_the_ID());?>&nbsp; &nbsp; <li data-href="<?php $trink = get_the_permalink(); echo preg_replace("#/$#im", '', $trink);?>">&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp;</li><?phpendwhile;?></ol>
打开App,查看更多内容
随时随地看视频慕课网APP