How to show posts from all categories on archive page on WordPress?

I want to show posts from all categories on the archive.php page. The way I made it, just works inside a category page. For example, If I access “myurl/category/test-category/” the page works correctly, but it only shows posts from “test category”. If I access “myurl/category/ it just returns a blank page.

My archive.php:

<?php get_header(); ?>
<div class="custom-container archive-wrapper">
    <?php if(have_posts() ): while (have_posts() ): the_post(); ?>
         <div class="blog-post-archive">
            <div class="img-archive-wrapper d-xl-flex">
                <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>" alt="Blog image" class="blog-image-archive">
                <div class="blog-post-archive-infos">
                    <h2 class="archive-title"><?php the_title(); ?></h2>
                    <span class="post-date-archive"><?php echo get_the_date(); ?></span>
                    <p><?php the_excerpt( ) ?></p>
                    <a href="<?php the_permalink();?>" class="see-more-posts"> Ver mais</a>
                </div>
            </div>
         </div>
         <?php endwhile; else: endif; ?>
     <div class="pagination__links">
    <?php the_posts_pagination( ); ?>
    </div>
  </div>
<?php get_footer(); ?>

Answer

Actually, the Category Templates works only to display posts from a defined category, so “example.com/category/news” will display all posts from the “News” category, this is the desired behavior.

If you want to display all posts on “example.com/category”, you have two options:

  • Just add a new page named “Category” go to Settings -> Permalinks, select “A static page” and choose your just created page “Category” as “Posts page”. It will show all your posts, from all categories;

  • If you don’t want to create a new page, add a file “page_category.php” on theme root, with the content:

<?php get_header(); ?>
<div class="custom-container archive-wrapper">
    <?php
      $uri = array_values(array_filter(explode('/', $_SERVER['REQUEST_URI'])));
      $current_page_number = is_numeric(end($uri)) ? end($uri) : (get_query_var('paged') ? get_query_var('paged') : 1);
      $wp_query = new WP_Query(array( 'post_type' => 'post', 'posts_per_page' => 10, 'paged' => $current_page_number ));
    ?>
    <?php if(have_posts()): while (have_posts()): the_post(); ?>
      <div class="blog-post-archive">
        <div class="img-archive-wrapper d-xl-flex">
            <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>" alt="Blog image" class="blog-image-archive">
            <div class="blog-post-archive-infos">
                <h2 class="archive-title"><?php the_title(); ?></h2>
                <span class="post-date-archive"><?php echo get_the_date(); ?></span>
                <p><?php the_excerpt() ?></p>
                <a href="<?php the_permalink();?>" class="see-more-posts"> Ver mais</a>
            </div>
        </div>
      </div>
    <?php endwhile; wp_reset_postdata(); else: endif; ?>
    <div class="pagination__links">
      <?php
        echo paginate_links( array(
          'total'        => $wp_query->max_num_pages,
          'current'      => max( 1, $current_page_number ),
        ) );
      ?>
    </div>
  </div>
<?php get_footer(); ?>

And, on your functions.php:

add_action('template_include', function( $template ) {
  $url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
  if ( preg_match('/^category(/page(/[0-9]+)+)?$/', $url_path) ) {
     // load the file if exists
     $new_template = locate_template('page_category.php');
     if ( '' != $new_template ) {
      return $new_template ;
    }
  }
  return $template;
});

You can also make it through a Page Template.

Leave a Reply

Your email address will not be published. Required fields are marked *