简体   繁体   中英

Posts_per_page category wordpress showing 1 post instead of multiple

I guess I am doing something wrong with my loop or my code is broke. I'd like to show all the posts from a specific category. Whatever I do, I only see 1 post appearing.

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

What am I doing wrong here?

Your endwhile; is in the wrong place: the code that builds your <li> tag is outside the loop which is why you're seeing only one post.

It should be:

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

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

<?php
endwhile;
?>
</ol>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM