Give Posts a Unique Style with WordPress Custom Post Templates

0
88

With the introduction of format varieties in WordPress, the flexibility to publish “Link” posts or “Gallery” posts , and many others. turned one thing you would do out of the field.

But should you’ve ever wished to present totally different posts a type of your personal, there are a couple methods to perform this—with a little bit of customized coding, or by utilizing a plugin.

We’ll take a look at each on this publish.

Continue studying, or use the hyperlinks beneath to leap to any part:

First up, let’s…

Create A Custom Post Template With Code

1. It’s a good thought to make a copy of your website earlier than fiddling with information and code, so go forward and do a fast backup now.

A superb host will doubtless have a built-in backup characteristic you could avail your self of. If you host with WPMUDEV, you possibly can run a fast backup by means of The HUB. Alternately, you would use a plugin (we suggest Snapshot Pro).

Backup files in the Hub
Push-of-a-button backups by way of WMUDEV’s The Hub Interface.

2. Access your WordPress website information, both by yourself server, or by means of your host entry level.

The Hub, view-edit files
Accessing WP website information by means of WPMUDEV’s Hub is a swift, streamlined expertise.

3. Navigate to: Files > wp-content > themes > twentytwenty (or no matter theme you’ll be utilizing)… and find the single.php or singular.php file.

4. Make a copy of this file, then rename it. I’m going to name mine: jbtest.php.

Duplicate & rename a php file in WP
Creating and modifying a php file in WordPress.

5. Now we have to transfer this file into the templates subfolder (underneath twentytwenty).

You’ll word that the information already populated listed below are preceded by “template–”, so we’ll rename the file once more to match that conference, making mine: template-jbtest.php.

Creating and editing a php file in WordPress.
This is how the folder and information take a look at this level.

Now that the file is called appropriately and within the right folder, we have to edit the contents so it’s coded as a template.

6.  Go into modifying mode (right-click the file, and select Edit textual content), and on the very high of the code, add the next:

<?php
/*
* Template Name: JBTest
* Template Post Type: publish, web page
*/
?>

Pasting the template code into the PHP file.

Where the code reads “Template Name Posts: JBTest”, use no matter title you want (rather than JBTest).

Note: Make positive every line of this high degree code is by itself row. (It might show in a single line while you paste it.)

7. Change the remainder of the file in no matter method you want, to make it your personal. Make positive to click on “Save & Close” after finishing the edit.

Insert test text php file
I added some random “testing” textual content additional beneath, as a visible validation that the code labored.

Create a New Post Using a Template

Let’s create a new publish, so we will take a look at out the template we simply made.

1. From the WordPress dashboard, create a New Post.

2. In the precise aspect menu, it’s best to see a column known as “Post Attributes”, with the header “Template” beneath it.

3. Click on the Template dropdown menu to see the various templates obtainable. Your new template might be listed there as you named it, so go forward and choose it!

New Template type in dropdown menu of Post Editor
Create your publish and reserve it, or just save and preview to get a take a look at it.
Preview of post made with custom template
Preview of a New Post made with my customized template.

There’s my “test” message. It’s not very fairly, however that’s simply mounted by formatting, corresponding to including padding, altering font sort, type, shade, and many others. The necessary factor is, it undoubtedly did what we wished!

Now, let’s strive the plugin technique.

Create a Custom Post Template with a Plugin

If you don’t need to contact any code, you possibly can decide to go the plugin route. In this instance, we’re going to make use of the Custom Layouts plugin.

Head to the WP plugin repository to get the Custom Layouts plugin.

Custom Layouts plugin
Custom Layouts plugin.

2. After putting in and activating the plugin, you’ll see its menu within the dashboard. Click on Templates, then the Add New Template button.

Custom Layouts plugin, add a template
Custom Layouts plugin, add a template.

3. This will open the Template modifying mode, which works with Gutenberg blocks by way of the left aspect of the display screen.

You can work from the Template or Element tabs, accessible on the precise aspect of the display screen. Make positive to call your template, and reserve it when full.

Custom Layouts plugin, Template editing mode.
Custom Layouts plugin, Template modifying mode.

4. You can save this as a template for any future posts, or use the shortcode (furthest proper aspect panel in modifying mode) to show a single occasion of the template.

If you like, you possibly can work from the Layouts menu, which permits for straightforward setup of grid sort posts. Everything you do right here will also be saved as a template for future use, or by utilizing shortcode to show the structure.

Custom Layouts plugin, Layout editing mode.
Custom Layouts plugin, Layout modifying mode.

5. You can View, Edit, or Delete Templates or Layouts at any time from the dashboard plugin submenus.

Custom Layouts plugin, saved templates list.
Custom Layouts plugin, saved templates listing.

Post-it Notes

There you’ve gotten it… two comparatively fast and straightforward methods to present particular person posts a distinctive type.

For those that notably take pleasure in DIY, try this in-depth article on the weblog, Creating Custom Content in WordPress: Custom Post Types.

Editor’s Note: This publish has been up to date for accuracy and relevancy.
[Originally Published: November 2011 / Revised: April 2022]

Tags:

Source link