I’ve been searching for a way to use a custom page template for my custom post types using a custom plugin, yup, took me hours. LOL

Filter

Yup, we can use the filter called single_template. Sample code below. 

add_filter( ‘single_template’, function($template) {
global $post;
if ( ‘areas-served’ === $post->post_type && locate_template( array( ‘single-areas-served.php’ ) ) !== $template ) {
   return RSGCH_TEMPS . ‘areas/single-areas-served.php’;
}
return $template;
} );

Breakdown
my-custom-post-type : The custom post type we want to target for the page template.

single-custom-page-template.php : Check if the page template we want to use is already existing in the child theme and or the main theme. 

plugin_dir_path( __FILE__ ) : The path location of your plugin. Not the URL.

single-areas-served.php : This would be the file we want to use for our custom post type pages. 

As you can see that’s pretty much it LOL

HAPPY CODING!!!!

~ click image at your own risk lol ~

Ryner Galaus

Ryner SG

An ordinary person with a tiny brain hoping to be useful in his own way. A homebody hoping to one day create and build his dreams while in service to people. An underachiever who was once called nobody slowly growing up. A web developer learning and earning his way through life. Ryner is what my family and friends call me, and I hope you do too. By the way, I am also affiliated with Frontrow International, so for inquiries or orders, message me.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *