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?

enter image description here

1 Answer 1

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;
}
?>
3
  • Thanks a bunch for the help @WebElaine, if my structure is as the image I just uploaded above, how would I structure that?
    – user155484
    Commented Jan 27, 2020 at 16:08
  • $page_template = dirname( __FILE__ _ . 'templates/single-profile.php'; That should do it. Commented Jan 27, 2020 at 16:17
  • Which of the files in your screenshot is the template? profile-card.php?
    – WebElaine
    Commented Jan 27, 2020 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.