admin管理员组

文章数量:1122826

I have implemented inside my plugin this hook to change the single template for custom post type (I do not want it to be in the child theme, just in my plugin) and it works just fine.

Now I want to implement content-single.php inside my plugin. How can it be done? This is the code that implements the custom template inside the plugin:

//Template fallback
function get_custom_post_type_template($single_template) {
    global $post;
    if ($post->post_type == 'my-custom-post-type') {
        $single_template = dirname( __FILE__ ) . '/single-my-custom-post-type.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'my-custom-post-type' );

I have implemented inside my plugin this hook to change the single template for custom post type (I do not want it to be in the child theme, just in my plugin) and it works just fine.

Now I want to implement content-single.php inside my plugin. How can it be done? This is the code that implements the custom template inside the plugin:

//Template fallback
function get_custom_post_type_template($single_template) {
    global $post;
    if ($post->post_type == 'my-custom-post-type') {
        $single_template = dirname( __FILE__ ) . '/single-my-custom-post-type.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'my-custom-post-type' );
Share Improve this question edited Feb 3, 2015 at 7:41 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Feb 3, 2015 at 7:20 DoriDori 1211 silver badge5 bronze badges 3
  • Do you mean that you have a custom single-content.php in your plugin that you wish to use, or that you want your plugin template to use the default content-single.php? If it's the first, look at locate_template(), if it's the second look at get_template_part(). If it's neither, please can you clarify. – David Gard Commented Feb 3, 2015 at 10:18
  • I am already load single_template and this is fine. But I would like to make custom template for the content itself (the title, body and the other parts of the custom post type). – Dori Commented Feb 3, 2015 at 14:21
  • Have a look at any of the default ones to see how they are done, there are plenty available in the twentyfourteen and twentyfifteen themes. – David Gard Commented Feb 3, 2015 at 16:02
Add a comment  | 

1 Answer 1

Reset to default 0

In case anyone will want to do this, this is what worked for me:

In my single-my_custom_post_type.php

I replaced this line:

get_template_part('content', get_post_format());

With this one:

include 'content-spp_service_provider.php';

本文标签: Contentsinglephp inside my plugin