{"id":516,"date":"2017-01-22T16:49:51","date_gmt":"2017-01-22T16:49:51","guid":{"rendered":"https:\/\/www.joetannorella.com\/?p=516"},"modified":"2017-01-22T18:52:46","modified_gmt":"2017-01-22T18:52:46","slug":"custom-template-custom-post-type-single-post-wordpress","status":"publish","type":"post","link":"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/","title":{"rendered":"Custom template for custom post type single post wordpress"},"content":{"rendered":"

I recently had\u00a0a somewhat unique situation whereby I needed a custom template per post of a custom post type.<\/p>\n

Consider the custom post type being ‘services’, then you’d set it like this:<\/p>\n

\r\n$labels = array(\r\n 'name' => 'Services',\r\n 'singular_name' => 'Service',\r\n 'menu_name' => 'Services',\r\n 'parent_item_colon' => 'Parent Service',\r\n 'all_items' => 'All Services',\r\n 'view_item' => 'View Service',\r\n 'add_new_item' => 'Add New Service',\r\n 'add_new' => 'Add New',\r\n 'edit_item' => 'Edit Service',\r\n 'update_item' => 'Update Service',\r\n 'search_items' => 'Search Service',\r\n 'not_found' => 'Not Found',\r\n 'not_found_in_trash' => 'Not found in Trash',\r\n );\r\n $args = array(\r\n 'label' => 'Services',\r\n 'description' => 'Service',\r\n 'labels' => $labels,\r\n 'hierarchical' => true,\r\n 'public' => true,\r\n 'show_ui' => true,\r\n 'show_in_menu' => true,\r\n 'show_in_nav_menus' => true,\r\n 'show_in_admin_bar' => true,\r\n 'menu_position' => 5,\r\n 'can_export' => true,\r\n 'has_archive' => true,\r\n 'exclude_from_search' => false,\r\n 'publicly_queryable' => true,\r\n 'supports' => array( 'thumbnail', 'editor', 'title', 'excerpt', 'page-attributes' )\r\n );\r\n \/\/ Registering your Custom Post Type\r\n register_post_type( 'Services', $args );\r\n<\/pre>\n

The URL structure follows something like:<\/p>\n

\/services\/web-development<\/p>\n

\/services\/consulting<\/p>\n

\/services\/cooking<\/p>\n

…and so on.<\/p>\n

In this example, I was looking to have a drastically different template for each of these\u00a0custom post type pages.<\/p>\n

The custom archive template for this is pretty simple, just create a file called ‘archive-services.php’. Similarly, the single post type template is simple, just create a file called ‘single-services.php’.<\/p>\n

Creating a post-specific template<\/h2>\n

The difficulty is where you need a specific template for each post, based on ID or slug.<\/p>\n

Usually you can do this for pages or posts by creating a file called ‘page-{slug}.php’ or ‘page-{ID}.php’ (in the case of the post being a page post type.<\/p>\n

To do the same thing for a custom post type is a little bit more complicated. In an ideal world you could simply create ‘single-{$posttype}-{$slug}.php’ – but alas this does not work. You need to create your own filter which bypasses the tempalte selection, based on the post_type name.<\/span><\/p>\n

In your functions.php file use the following:<\/p>\n

\r\n<p class="lang-php prettyprint prettyprinted" style="color: #393318;"><code><span class="pun" style="color: #303336;">add_filter( 'single_template', function( $template ) {\r\nglobal $post;\r\n$post_type_name = 'services';\r\nif ( $post->post_type === $post_type_name ) {\r\n$locate_template = locate_template( "single-".$post_type_name."-{$post->post_name}.php" );\r\nif ( ! empty( $locate_template ) ) {\r\n$template = $locate_template;\r\n}\r\n}\r\nreturn $template;\r\n} );\r\n<\/pre>\n

Don’t forget to replace ‘services’ with the name of your own post_type!<\/p>\n

Using this method, you’ll now be able to create a unique template for each of your custom post type posts.<\/p>\n

Further reading:\u00a0https:\/\/developer.wordpress.org\/themes\/basics\/template-hierarchy\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

I recently had\u00a0a somewhat unique situation whereby I needed a custom template per post of a custom post type. Consider the custom post type being ‘services’, then you’d set it like this: The URL structure follows something like: \/services\/web-development \/services\/consulting \/services\/cooking …and so on. In this example, I was looking to have a drastically different…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"yoast_head":"\nCustom template for custom post type single post wordpress - JoeTannorella.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom template for custom post type single post wordpress - JoeTannorella.com\" \/>\n<meta property=\"og:description\" content=\"I recently had\u00a0a somewhat unique situation whereby I needed a custom template per post of a custom post type. Consider the custom post type being ‘services’, then you’d set it like this: The URL structure follows something like: \/services\/web-development \/services\/consulting \/services\/cooking …and so on. In this example, I was looking to have a drastically different...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"JoeTannorella.com\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-22T16:49:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-01-22T18:52:46+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/\",\"url\":\"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/\",\"name\":\"Custom template for custom post type single post wordpress - JoeTannorella.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.joetannorella.com\/#website\"},\"datePublished\":\"2017-01-22T16:49:51+00:00\",\"dateModified\":\"2017-01-22T18:52:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.joetannorella.com\/#website\",\"url\":\"https:\/\/www.joetannorella.com\/\",\"name\":\"JoeTannorella.com\",\"description\":\"Web Development | Digital Marketing | Business\/Startups | Analytics\/data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.joetannorella.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.joetannorella.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom template for custom post type single post wordpress - JoeTannorella.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Custom template for custom post type single post wordpress - JoeTannorella.com","og_description":"I recently had\u00a0a somewhat unique situation whereby I needed a custom template per post of a custom post type. Consider the custom post type being ‘services’, then you’d set it like this: The URL structure follows something like: \/services\/web-development \/services\/consulting \/services\/cooking …and so on. In this example, I was looking to have a drastically different...","og_url":"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/","og_site_name":"JoeTannorella.com","article_published_time":"2017-01-22T16:49:51+00:00","article_modified_time":"2017-01-22T18:52:46+00:00","author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/","url":"https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/","name":"Custom template for custom post type single post wordpress - JoeTannorella.com","isPartOf":{"@id":"https:\/\/www.joetannorella.com\/#website"},"datePublished":"2017-01-22T16:49:51+00:00","dateModified":"2017-01-22T18:52:46+00:00","author":{"@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.joetannorella.com\/custom-template-custom-post-type-single-post-wordpress\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.joetannorella.com\/#website","url":"https:\/\/www.joetannorella.com\/","name":"JoeTannorella.com","description":"Web Development | Digital Marketing | Business\/Startups | Analytics\/data","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.joetannorella.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/84eafed4ad6cd6934b006e371c06c7ae","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.joetannorella.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/257001162d06da1a465f242dfa80dd7b?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.joetannorella.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/516"}],"collection":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/comments?post=516"}],"version-history":[{"count":6,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/posts\/516\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joetannorella.com\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}