Many of us find it very difficult when it comes to have custom post template. Even if you create a custom post template, the question comes how would you define for your custom post type?
I have written a quick and handy tutorial for using a custom post template based on your custom post type.
First, we will add a template redirect call in your theme’s function.php file
1 2 3 |
add_action('template_redirect','CustomPostTemplate'); // CustomPostTemplate is a function which are going to define below |
Now, let’s prepare our function for calling the custom template
1 2 3 4 5 6 7 8 9 10 11 |
function CustomPostTemplate() { global $post, $wpdb; $mypostid = $post->ID; $this_post_type = get_post($post->ID); $this_post_type_is = $this_post_type->post_type; if ($this_post_type_is == 'event') { include(get_template_directory()."/my_post_template.php"); exit; } } |
First, we get the post_type by calling get_post() function.
Under the IF condition, we are including the actual post template file. The function get_template_directory() is a built-in WordPress function which calls the full physical path of your active theme directory and then we call the file from it.
Hope it helps