WordPress display all category and posts on a page

WordPress display all category and posts on a page
WordPress display all category and posts on a page

How to display selected category or all categories lists with category post. Here is step by step guide to get WordPress category, its post and display in specific format. Let’s start process step by step for fetch WordPress category, its post and display on page.

Let’s start process step by step for fetch WordPress category, its post and display on page.

Step 1: Get wordpress category array:

For more detail and get category based on option argument visit below reference link for get_categories().
Codex
Code Reference

$cats = get_categories();

Step 2: Get categorywise post:

Make loop on category array.
Get category id.
Get category all posts using query_posts() and display with making loop on result.
For more option and detail of query_posts() check below links.
Codex
Code Reference

If you want to display limited record of category post use ‘showposts’ parameter in query_posts().

foreach ($cats as $cat):
    $cat_id = $cat->term_id;
    echo $cat->name;
    query_posts('cat='.$cat_id.'&showposts=5');
    if (have_posts()) :
        while (have_posts()) :
            the_post();
            the_title();
            the_permalink();
        endwhile;
    endif;
endforeach;

Full code for display all category posts with PHP and HTML

<?php
$cats = get_categories();
foreach($cats as $cat):
?>
    <div>
    <?php $cat_id = $cat->term_id; ?>
    <h2 class="title"><?php echo $cat->name; ?></h2>
    <hr/>
    <?php query_posts('cat='.$cat_id.'&showposts=5'); ?>
        <?php if(have_posts()) : while (have_posts()) : the_post(); ?>
            <a href="<?php the_permalink() ?>"
            title="<?php the_title(); ?>"
            rel="bookmark">
            <?php the_title(); ?>
            </a><br/>
        <?php endwhile; ?>
        <div class="readMore">
            <a href="<?php echo get_category_link( $cat->term_id ); ?>"
            title="<?php $cat->name; ?>" rel="bookmark">
            Show me more
            </a>
        </div>
        <?php endif; ?>
    </div>
<?php endforeach; ?>

That’s it, hope you like article and code will useful for your future development.