If you’re using post formats in your WordPress themes, then you might want to use a custom template for posts formats to make them look more meaningful. In this tutorial, I’ll show you how to use custom template files for posts formats in your WordPress blog. I’ll show you two different ways to do that.
Add Post Formats Support To Your Theme
In order to use post formats in your WordPress posts, you need to add format support to your current theme (if it doesn’t exist). You can head over to this link for more info.
Using Post Loop (Level: Experienced)
If you just want to change the loop of your format posts, then this trick is for you. We will use a single.php file with multiple loops for various formats.
Add following code to your single.php‘s loop:
<?php get_template_part( 'content', get_post_format() ); ?>
Code language: HTML, XML (xml)
Now, create and upload your custom format loops files to the root of your current theme. Evert post format template file should be named content-{post-format}.php. Example: content-video.php and content-audio.php
Also, don’t forget to add a content.php file as well, because it will be the default file to fall back to as a default.
Using Custom Single.php File (Level: Newbie)
If you’re a newbie or don’t want to mess with your post loop, then this trick is for you. We will create a custom template file for Video format, and will be naming it single-video.php.
Tip: You can also copy your single.php file to this template and make some changes that you want in the custom template.
Add your single-video.php to your current theme’s root and drop the following snippet to functions.php file:
add_action('template_include', 'load_single_template');
function load_single_template($template) {
$new_template = '';
// single post template
if( is_single() ) {
global $post;
// template for post with video format
if ( has_post_format( 'video' )) {
// use template file single-video.php for video format
$new_template = locate_template(array('single-video.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}
Code language: PHP (php)
Now, all posts with video format will use single-video.php file as its template.
Update
You can also use the following snippet to add support for format specific template for all formats:
function use_post_format_templates_27425( $template ) {
if ( is_single() && has_post_format() ) {
$post_format_template = locate_template( 'single-' . get_post_format() . '.php' );
if ( $post_format_template ) {
$template = $post_format_template;
}
}
return $template;
}
add_filter( 'template_include', 'use_post_format_templates_27425' );
Code language: PHP (php)
Now, just use single-{format name}.php format for the formats that you’re using.
hey hardeep, i wont to edit code of standard post format and video post format can you tell where to find this code. i already search in theme stype css files, in post format php file an too but nothing found.