I am following a rather outdated tutorial on custom post types and the last step is to create a custom page template, where this is intended to show all posts of the custom type in archive listing format. As per the comments, the methods used no longer work 6 years later (go figure) and my custom page template isn't an option when creating a new page.
If I place my custom template in the theme folder it works but the option to load it from within the plugin directory has broken. How can I fix this so I can assign the template to a new page?
Edit
As I better understand now, this code is only loading the template when viewing my custom post types. The confusion was due to it showing as an available template on the "Page Attributes" section when it was located in the Themes directory. I am wanting to have it available as an option when making a new page, but have the template file located in my plugin's directory.
The original code from the tutorial:
add_filter( 'template_include', 'include_template_function', 1 );
function include_template_function( $template_path ) {
if ( get_post_type() == 'movie_reviews' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'single-movie_reviews.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/single-movie_reviews.php';
}
}
}
return $template_path;
}
single-movie_reviews.php
is in plugin root directory ? Is the pasted code in the plugin too ? (I think that's not the problem butplugin_dir_path
return the path with a/
in the you can remove the/
from$template_path
)is_single
?add_filter( 'template_include', 'include_template_function', 1, PHP_INT_MAX);