templates - How do I find a way to create a global single.php for a custom post type?

admin2025-06-04  0

I am working on making a custom post type plugin for my website. However, I want to make it universal for any theme I may decide to change. Or to be able to append the post type to a single post file already.

It's a press release post type, and I am unable to find any guide to how to append or create a universal template file for the single post file.

Any guides or help would be much appreciated.

I am working on making a custom post type plugin for my website. However, I want to make it universal for any theme I may decide to change. Or to be able to append the post type to a single post file already.

It's a press release post type, and I am unable to find any guide to how to append or create a universal template file for the single post file.

Any guides or help would be much appreciated.

Share Improve this question edited Jan 4, 2019 at 2:44 Johansson 15.4k11 gold badges44 silver badges80 bronze badges asked Jan 4, 2019 at 2:39 xensorxensor 1
Add a comment  | 

1 Answer 1

Reset to default 1

There re various filters you can use to inject your custom template. One being the template_include, the other single_template, and even type_template.

The easiest one would be single_template in your case (example from codex):

function get_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'my_post_type') {
          $single_template = dirname( __FILE__ ) . '/post-type-template.php';
     }
     return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749046938a315902.html

最新回复(0)