In WordPress Get All Posts On A Page
In Wordpress, get all posts is kind of a difficult operation without some help. Here’s a walkthrough on how I get it done on this site.
February 2, 2018
In my Select All Posts and Categories from a WordPress Database post, I talked about how to grab all of your posts (titles and category names) in a command line, then do what you want with them. What if you want to have WordPress get all posts on a page? Many thanks to http://www.wpbeginner.com and their How to Display All Your WordPress Posts on One Page for getting me going. Between their walkthrough and some research at wordpress.org, I was able to get what I needed done. Here’s how I did it…
I first created a new page template. You’ve got, in most templates, a page.php. Copy that file to a new one, called whatever you want. I named mine page_all_posts.php. Just remember no spaces, and it should end in .php (dot, then lowercase php) and you’re going to need a text editor to make changes to it. I’d recommend Geany, since it’s cross-platform, but there are lots of them out there
My whole page.php file is here:
<?php
/**
* @package WordPress
* @subpackage fossfolks.com
*/
?>
<?php get_header(); ?>
<div id="left-column">
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="storytitle"> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div><!-- left-column -->
<?php get_sidebar();?>
<?php get_footer(); ?>
And since my left-column div is where I want my content to be displayed (my right columns are always things either in the category of the current post, or from the News category) I’m only going to be dorking with that area.
The first few lines of the new file (mine anyway) get edited to go like this:
<?php
/**
* @package WordPress
* @subpackage fossfolks.com
*/
?>
<?php /* Template Name: Show All Posts Page */ ?>
<?php get_header(); ?>
Different themes have different headers in their php files, but my line saying fossfolks.com is probably going to have YOUR theme’s title in it. Since you copied over from page.php, yours should be all set. You probably only need to worry about the line before the get_header line. You’ve got to have a Template Name: blahdy blah in there so that you can find this thing later from the WordPress interface. Mine says Show All Posts Page.
You can, in WordPress, get all posts with a few different methods. There are a couple plugins that can supposedly do it for you, but I find that this is easier in the long run. I’m just going to use a little PHP and html to spit out two columns. One has post titles in it, and one has the categories those posts are in.
<?php
/**
* @package WordPress
* @subpackage fossfolks.com
*/
?>
<?php /* Template Name: Show All Posts Page */ ?>
<?php get_header(); ?>
<div id=”left-column”>
<!-- ------------------------------------- -->
<!-- ALL my stuff is going in here -->
<?php
<!-- The New Query -->
$wpb_all_query = new WP_Query(array(‘post_type’=>’post’, ‘post_status’=>’publish’,’nopaging’ => true,
‘category__not_in’ => array( 7, 10, 25), ‘orderby’ => ‘category’)); ?>
<!-- Note the category_not_in blurb. I don’t want, in this hypothetical exercise,
to pull posts from categories 7, 10, or 25. These might be private posts,
experimental categories not ready for prime time yet, etc. -->
<?php if ( $wpb_all_query->have_posts() ) : ?>
<h2> All Posts at Fossfolks.com</h2>
<table class=”allPosts”>
<tr>
<td><h3>Post Title</h3></td>
<td class=”right”><h3>Category</h3></td>
</tr>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<tr>
<td><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></td>
<td class=”right”>
<?php
$category = get_the_category();
echo '<a href=”'.get_category_link($category[0]->cat_ID).'”>’ . $category[0]->cat_name . '</a>’;
?>
</td>
</tr>
<?php endwhile; ?>
</table>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.’ ); ?></p>
<?php endif; ?>
<!– ——————————————- –>
</div><!– left-column –>
<?php get_sidebar();?>
<?php get_footer(); ?>
The next step is to go make a new page in WordPress (Pages -> Add New). Once you’re there, give the page a title that makes sense (I named mine All Posts) and go pick a template. Grab the one you just created.
This should contain what you stuck on about line 7 of your new template file.
That’s pretty much it. Save this page without anything but a title, and you’re golden I think. Putting a link to this page is another matter, but simple. In the menu editor, you should see it and be able to drag it in. I actually haven’t decided yet where I want a link to mine; I’m thinking of having an archive link near about and contact. But for those of you wondering how I was able to, in WordPress, get all posts displayed, have a gander over here at my All Posts page.
NextPrevious
Leave a Reply