最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Use custom template on custom post type

programmeradmin3浏览0评论

So I'm kinda stuck on something that I'm trying to achieve and unsure how to even target it, so below I will explain all that I have and what I have tried.

So I'm using the Genesis Framework and a child theme. I'm trying not to touch the Framework or the child theme, and do all the development inside mu-plugins but I'm having trouble targeting a custom template.

So I've created a custom post type that is linked as /profile/ so let's say I create a file called page-profile.php, following WordPress hierarchy it will use my custom page first.

But how do I target a template that has a URL such as /profile/jake-ferguson/ where the author's name will be different each time?

So I'm kinda stuck on something that I'm trying to achieve and unsure how to even target it, so below I will explain all that I have and what I have tried.

So I'm using the Genesis Framework and a child theme. I'm trying not to touch the Framework or the child theme, and do all the development inside mu-plugins but I'm having trouble targeting a custom template.

So I've created a custom post type that is linked as /profile/ so let's say I create a file called page-profile.php, following WordPress hierarchy it will use my custom page first.

But how do I target a template that has a URL such as /profile/jake-ferguson/ where the author's name will be different each time?

Share Improve this question edited Jan 27, 2020 at 16:10 asked Jan 27, 2020 at 13:43 user155484user155484
Add a comment  | 

1 Answer 1

Reset to default 1

If you're just trying to add a template within the theme, name your file single-profile.php and that will apply to all single "profile" CPTs.

But you mentioned you don't want to touch the child theme. You could instead create a plugin and use a page_template filter to use your plugin's single-profile.php template instead of any of the theme templates.

You would create a plugin folder, put your single-profile.php file in that folder, and add a plugin.php file that contains something like

<?php
add_action('page_template', 'wpse_apply_profile_template');
function wpse_apply_profile_template() {
    // If this is the CPT we're looking for
    if(is_singular('profile')) {
        // Apply this plugin's "single-profile.php" template
        $page_template = dirname( __FILE__ . 'single-profile.php';
    }
    // Always return, even if we didn't change the template
    return $page_template;
}
?>
发布评论

评论列表(0)

  1. 暂无评论