I created three plugins that define a custom post type and use template_include
to include the template for the single post version.
For some reason, the latest one that I have built creates the records properly, but can't seem to find the template. The code is literally copied-and-pasted, so I'm not sure why this is the case. The only changes were to the names of the functions and post types to avoid conflicts.
Here is the part that adds the template_include
filter:
function include_person_template_function($template_path)
{
if (get_post_type() == 'featured_people') {
if (is_single()) {
if ($theme_file = locate_template('single-featured-person.php')) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path(__FILE__) . 'single-featured-person.php';
}
}
}
return $template_path;
}
add_filter('template_include', 'include_person_template_function', 1);
Since the admin works fine, the only other part that could be the problem is where the post type is defined:
function create_featured_people()
{
register_post_type('featured_people', array(
'labels' => array(
'name' => 'Featured People',
'singular_name' => 'Featured Person',
'add_new' => 'Add New',
'add_new_item' => 'Add New Featured Person',
'edit' => 'Edit',
'edit_item' => 'Edit Featured Person',
'new_item' => 'New Featured Person',
'view' => 'View',
'view_item' => 'Featured Person',
'search_items' => 'Search Featured People',
'not_found' => 'No Featured People Found',
'not_found_in_trash' => 'No Featured People Found in Trash',
'parent' => 'Parent Featured Person'
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( '' ),
'has_archive' => true
));
}
A few other things to note:
- I have ruled out a caching issue.
- The other plugins use different function names and CPT names.
Any help or insights would be greatly appreciated!!
create_featured_people
hooked toinit
? note that there's also thesingle_template
filter that fires for just singular post types, which will simplify your code a bit and not run on every other type of request.