How To Set Up WordPress Custom Posts And Display On Page And Post

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:

<?php
/**
 * @package WordPress
 * @subpackage U-Design
/**
/**
 * Template Name: Articles
 **/
get_header(); ?>
<?php
    $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(); ?>
<header class="entry-header">
<h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink(); ?>"> Read More ... </a>
</p>
</div>
<?php endwhile; else : ?>
<p>There are no articles</p>
<?php endif;
wp_reset_postdata(); ?>
<?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.

The tutorials on pagination on wordpress custom posts are at

http://callmenick.com/post/custom-wordpress-loop-with-pagination and
http://designphiliconline.blogspot.in/2012/08/wordpress-custom-post-type-pagination.html

Show Categories and tags in custom posts

To enable all custom posts to have the same tags and categories as the normal posts, add the following code to the theme’s functions.php file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'articles'); // don't forget nav_menu_item to allow menus to work!
    $query->set('post_type',$post_type);
    return $query;
    }
}

Archives.php only shows content of type ‘post’, but you can alter it to include custom post types. Add this filter to your functions.php file. This is revealed by csstricks.

How to use custom category template (category.php) for different custom post type?

Answer is provided here:

You can based on the post type like so: say you have category.php for regular posts and your-custom-type-category.php for your custom post type posts then just add this to your regular category.php at the very top.

<?php if (have_posts()){
    the_post();
    if( 'your_custom_type' == get_post_type( $post ) ) {
        rewind_posts();
        get_template_part('your-custom-type-category');
        exit();
    }
    rewind_posts();
}
?>

and now when ever a category is requested it will check for your custom post type and if found it will use your your-custom-type-category.php as template and all regular posts will display using category.php with no change.

See also (1) and (2)

Leave a Reply

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