I’m writing this post as I’ve found out a useful technique recently and wanted to share it before I forget how it works!
When you write a post, you can mark it with a category. I use these categories to organise my posts into topics (definition of categories vs. tags):
- Web design
- Market research
- Marketing
- Mindset
- Poetry
- Short stories
By default all posts in all of these categories will appear in the list of posts on my home page, in the order in which they were posted. But mixing poetry and short stories in with articles on market research and web design didn’t seem particularly organised to me, so I wondered if there was a way to separate them out.
Turns out that there is. It is possible to display posts from different categories on separate pages, and to prevent posts from certain categories from being displayed on the home page.
To display posts from one category on a page
Make a copy of the template for a standard page and name it something like “page of posts”. Open it in a text editor, and insert this at the very start of the file:
<?php
/*
Template Name: page-of-posts
*/
?>
Save the file in the same folder as the rest of your templates. Having included this comment at the start of your file will mean that WordPress recognises it as a page template, so it will now appear as a selectable option when you create or edit your pages. It will also appear in the list of files that you see if you click Appearance then Editor in the main right hand menu.
To get a page to display the posts from a particular category, find the place just before the end of the container div:
THIS PLACE RIGHT HERE!
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
and add this markup (from a helpful forum post by stvwlf):
<?php
$catID = 0;
if (is_page('poetry')) {
$catID=6;
} elseif (is_page('short-stories')) {
$catID=9;
}
if ($catID) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=$catID&paged=$paged");
} ?>
The value you need to put into the is_page function is the page slug of the page you want the category to show up on – you can find this below the page name when you are editing your page.
Then you need to go to the template for your index page, and find the part that makes the loop run to output the posts. In my theme this looks like this:
<?php
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*/
get_template_part( 'loop', 'index' );
?>
Copy this and paste it into your template just before the end of the container div. Now you should be done. I assigned my “page of posts” template to my poetry page, published a couple of posts with the category poetry, and they showed up just fine!
Having got these posts showing up in their own sections, I then wanted to prevent them from showing up on my home page. Here is a description of how to manage that:
How to exclude categories from your home page
Finally, to avoid my home page looking neglected when I’m doing a lot of creative writing, I thought it would be a good idea to add an RSS feed onto the home page showing my latest poems. This was easy once I figured out the feed address for the category. Info about WordPress feeds.