I couldn’t find any simple guides on how to set up custom posts and display them on the archives page and on single posts.
Custom posts are necessary if you want to display two different types of posts in the same wordpress installation.
E.g. suppose you have a site which has posts about movies. The template will be designed to set out information relevant to movies such as the actors’ names, the storyline, etc, etc.
If you want to write a series of posts on another subject, say cooking, using the template files that are used for movies will obviously not work.
This problem can be easily resolved by the use of custom posts.
Step 1: Install a customs post plugin:
The two most popular plugins are “WCK - Custom Fields and Custom Post Types Creator” and “Custom Post Type UI”.
I am using WCK - Custom Fields and Custom Post Types Creator for this demo.
Step 2: Go to WCK > Post Type Creator.
Create a post type called “articles”. Give it the singular label ‘article’ and the plural label “articles (you can choose any other name you want but remember to use that name in the template files as well).
Under "supports", you can leave it at default or choose any or all of the options. I chose "excerpt". You can come back later and change the options.
Clicking the “add entry” button will create an “articles” menu on the left side. Go there and add two articles called articles 1 and articles 2 with some arbitrary text in them. Open the articles to ensure that you are able to view them. If there is a 404 'not found' problem, it is probably because of a permalinks problem. Go to Settings > Permalinks and refresh the permalinks. You can also change the permalinks to default and then go back to the pretty permalinks.
I am using a nginx server built with easyengine. The wordpress pretty permalinks work effortlessly.
Step 3: Create a template page to show all the custom posts grouped under ‘articles’.
Create a new file in notepad called “page-articles.php” and paste the following code in it:
* @package WordPress
* @subpackage U-Design
* Template Name: Articles
$args = array(
'post_type' => 'articles',
'posts_per_page' => 10,
'orderby' => 'rand'
$the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink(); ?>"> Read More...</a>
<?php endwhile; else : ?>
<p>There are no articles :( </p>
<?php get_footer(); ?>
Upload the file to the folder which has the “page.php” template.
Now, go to Pages > Add New Page in the dashboard. Name the page whatever you want, such as “My Latest Articles” or anything else. In the “Template” section to the right, choose “Articles” from the drop down menu.
Publish the page and view it. You should be able to see the two articles titled “Article 1” and “Article 2” in it.
You can modify the look and feel of the page by tinkering with the template file called “page-articles.php” that we created earlier.
Step 4: Create a template file to show the single posts:
If you click each article, it will display in the default single page template.
To change this, download the “single.php” file. Open it in notepad and make the changes you want. Thereafter rename the file as “single-articles.php” and upload it to the same folder in which single.php resides.
Clear the cache (if you have WP Super Cache) and refresh the single post, you should be able to view the post in the new single post template.
The logic is that Wordpress will display all pages and posts in the default page.php and single.php templates respectively. However, if you create a new type of custom post and append the name of that post to a new page and single post template, Wordpress will display them in that template.
Now, you can create a menu item to append the articles. It will display separately from the other posts.
So, it is that simple to have custom posts thanks to wordpress and the plugins.