How to create Custom Post Template for Specific Category in WordPress ?
Creating Custom Category Post Template in WordPress ?
Some bloggers might come under situation where they want different post template for specific category.There are many more plugins still this method will explain simplest method to create post template for particular category in step by step manner.
Steps to Create Post Template for Specific Category :
- In WordPress System we have default post template as “single.php“.
- “single.php” template file is used for each and every post of any category by default . (our aim is to make n number of templates for n categories)
- Log in to your Control Panel.
- Navigate to your directory. Suppose your wordpress is installed on following directory
1 |
public_html<span style="color: #308080">/</span>domain<span style="color: #308080">.</span>com<span style="color: #308080">/</span>blog |
then go to this following directory -
1 |
public_html<span style="color: #308080">/</span>domain<span style="color: #308080">.</span>com<span style="color: #308080">/</span>blog<span style="color: #308080">/</span>wp<span style="color: #308080">-</span>content<span style="color: #308080">/</span>wp<span style="color: #308080">-</span>theme<span style="color: #308080">/</span>theme_name |
- Now your are in theme directory. Search for single.php file.
- Next step is to make two copies of single.php. in same directory and rename them so that we have following files with us.
1 2 3 |
<span style="color: #308080">.</span><span style="color: #308080">.</span><span style="color: #308080">/</span>wp<span style="color: #308080">-</span>theme<span style="color: #308080">/</span>theme_name<span style="color: #308080">/</span>single<span style="color: #308080">.</span>php <span style="color: #308080">.</span><span style="color: #308080">.</span><span style="color: #308080">/</span>wp<span style="color: #308080">-</span>theme<span style="color: #308080">/</span>theme_name<span style="color: #308080">/</span>single_google<span style="color: #308080">.</span>php <span style="color: #308080">.</span><span style="color: #308080">.</span><span style="color: #308080">/</span>wp<span style="color: #308080">-</span>theme<span style="color: #308080">/</span>theme_name<span style="color: #308080">/</span>single_default<span style="color: #308080">.</span>php |
- “single_google.php” template will be used for all posts comes under category “Google”.
- “single_default.php” template will be used for all remaining posts. (original).
- Now go and edit single_google.php as per requirement. If require user is free to change complete template.
- After finishing we now have to edit single.php because still our template is using default template file … i.e singe.php .
- Add following code in single.php -
Add this code to single.php :
1 2 3 4 5 6 7 8 9 |
<span style="color: #7f0055;background: #ffffe8"><?php</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #000000;background: #ffffe8">$post</span><span style="color: #000000;background: #ffffe8">=</span><span style="color: #000000;background: #ffffe8">$wp_query</span><span style="color: #000000;background: #ffffe8">-></span><span style="color: #000000;background: #ffffe8">post</span><span style="color: #000000;background: #ffffe8">;</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #7f0055;background: #ffffe8;font-weight: bold">if</span><span style="color: #000000;background: #ffffe8">(</span><span style="color: #000000;background: #ffffe8">in_category</span><span style="color: #000000;background: #ffffe8">(</span><span style="color: #2a00ff;background: #ffffe8">'google'</span><span style="color: #000000;background: #ffffe8">)</span><span style="color: #000000;background: #ffffe8">)</span><span style="color: #000000;background: #ffffe8">{</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #7f0055;background: #ffffe8;font-weight: bold">include</span><span style="color: #000000;background: #ffffe8">(</span><span style="color: #000000;background: #ffffe8">TEMPLATEPATH</span><span style="color: #000000;background: #ffffe8">.</span><span style="color: #2a00ff;background: #ffffe8">'/single_google.php'</span><span style="color: #000000;background: #ffffe8">)</span><span style="color: #000000;background: #ffffe8">;</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #000000;background: #ffffe8">}</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #7f0055;background: #ffffe8;font-weight: bold">else</span><span style="color: #000000;background: #ffffe8">{</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #7f0055;background: #ffffe8;font-weight: bold">include</span><span style="color: #000000;background: #ffffe8">(</span><span style="color: #000000;background: #ffffe8">TEMPLATEPATH</span><span style="color: #000000;background: #ffffe8">.</span><span style="color: #2a00ff;background: #ffffe8">'/single_default.php'</span><span style="color: #000000;background: #ffffe8">)</span><span style="color: #000000;background: #ffffe8">;</span> <span style="color: #000000;background: #ffffe8"> </span><span style="color: #000000;background: #ffffe8">}</span> <span style="color: #7f0055;background: #ffffe8">?></span> |
means we are just using if else condition to check out the category of the post and we will redirect user to particular post template.