1

I have Create a page that lists posts by category but only shows 5 posts from each category. how do I add a button in each category to display all posts?

Here's my code for a page that lists posts by category:

$categories = get_terms( 'category', 'orderby=name&order=ASC');

    foreach ( $categories as $category ) {

        echo '<h2 class="post-title">' . $category->name . '</h2>';
        echo '<div class="post-list">';

        // WP_Query arguments
        $args = array(
            'cat'           => $category->term_id,
            'orderby'       => 'term_order',
        );

        // The Query
        $query = new WP_Query( $args );

        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
        ?>
            <p><a href="<?php the_permalink();?>"><?php the_title(); ?></a></p>
        <?php

            } // End while
        } // End if

        echo '</div>';

        // Restore original Post Data
        wp_reset_postdata();

    } // End foreach
2
  • 1
    Have a look at the docs - developer.wordpress.org/reference/classes/wp_query - add 'posts_per_page' => -1 to the $args.
    – WebElaine
    Dec 2 '19 at 20:36
  • thx for answer, do you mean like this: $args = array( 'cat' => $category->term_id, 'posts_per_page' => -1, 'orderby' => 'term_order', ); I mean how to add code ( 'posts_per_page' => -1,) by clicking link or button. So will show all post. Can you help me!
    – Julham
    Dec 3 '19 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.