Creating a WordPress All Posts Page

I wanted a page that would list all my posts, a site index basically. I tried a simple method and it was close but not what I wanted, it did not dynamically put the list of posts inside the content area of a page, so it looked very odd.

So here is a way to get your “All Posts” looking how it should on your WordPress site.

Example: http://tripsintech.com/all-posts/

Step 1 Child Theme

Make sure you are using a child theme on your WordPress site.
( You don’t have to do this but if you don’t use a child theme then when you update your theme then anything you do next will just be overwritten )

Step 2 Page Template

Create a page template. To do this create a php file in your active theme directory, mine is here:
“…/wp-content/themes/twentyfifteen-child/AllPostTemplate.php”

Now paste in the below code into  your AllPostTemplate.php file and save it.

<?php
/*
Template Name: All posts
*/
get_header(); ?>
 <div id="primary" class="content-area">
 <main id="main" class="site-main" role="main">

 <?php
 // Include the page content template.
 get_template_part( 'content', 'page' );
 
 $first = 0; //The first article to be displayed
 while(have_posts()) : the_post(); 
 ?>
<script>
 var x = document.getElementsByClassName("entry-content");
 if (x[0] != null) {
 x[0].innerHTML = "<ul> <?php $myposts = get_posts('numberposts=-1&offset=$first'); foreach($myposts as $post) : ?><li><?php the_time('d/m/Y') ?>: <a href='<?php the_permalink(); ?>'><?php the_title(); ?></a> </li><?php endforeach; ?></ul> <?php endwhile; ?>";
 }
</script>

 </main><!-- .site-main -->
 </div><!-- .content-area -->

<?php get_footer(); ?>

Step 3 Create Page

Now we need to actually create the all posts page, this is very simple and is done through the WordPress dashboard.

Go to pages, then create a new page and change the Page Template from default (or whatever else it is) to be “All posts”.

All done!

 

 

NB: This template has only been tested with the TwentyFifteen Theme, it should work for most though.

Leave a Reply

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