How to create custom Post Templates with Wordpress - gHacks Tech News

How to create custom Post Templates with WordPress

Sometimes you may want to customize the post template of a WordPress post. This can be the case for instance when you want to add something to the page the post is displayed on that should not appear on other post pages, or if you want to remove something, e.g. advertisement.

In the past, we have shown you how you can create custom tag pages and custom headers for categories which can be very helpful in this regard as well. These custom pages allow you for instance to add unique content to important pages to provide visitors with additional information.

The WordPress plugin Custom Post Template is ideal for our purpose. While it still requires you to add a new php file to your WordPress theme directory manually, you can afterwards use it comfortably from within the WordPress admin dashboard.

First thing that you need to do is install the Custom Post Template plugin on the WordPress site that you want to use it on.  I have tested it on the latest available version, 3.4.1 at the time of writing, and it is working without issues there. Once you have installed and activated the plugin in the dashboard, fire up an ftp program and open the WordPress theme folder in it.

Now, the easiest way to create templates is to copy and rename the single.php file of your WordPress theme. You can name it any way you want, but I'd suggest you select a name that is easily identifiable.

Once done, edit the newly created file and add the following information at the very top of it before any other content:

<?php
/*
Template Name Posts: noads
*/
?>

Replace the noads name with a name that you can identify the template by.  You can now make modifications to the post template, like removing ads or adding advertisement to it. Save the file afterwards and repeat the process for as many template files as you want.

When you now open an existing post in the dashboard or write a new one, you will see a Post Template module on the page that you can use to switch from the default WordPress template to a newly created one.

post template wordpress

I'd suggest you verify that everything is in order by previewing the post before you publish it on the site. This is especially important at the first time to make sure that everything has been configured correctly in the template file.

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. Mike said on July 10, 2012 at 10:25 pm
    Reply

    Thanks for this! custom post types are one of the most useful tools!

  2. mark shirley said on July 26, 2012 at 2:24 pm
    Reply

    But it strips out the styling of twenty eleven child theme how can I keep this

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.