Create Template for Custom Post Type same like for Page

I’m making my first custom theme using custom post types and I’ve encountered one problem.
I’d like to make some templates like you can make for Pages (i.e. page-fullwidth.php page-noslider.php etc.)
I know if you want to make template for custom post type you just make for example single-cookingrecipes.php but I’d like to make single-cookingrecipes-fullwidth.php and some more.

I have created 2 such files and added

Read More
<?php
/*
Template Name: Art
*/
 ?>

and still no luck 🙁

Related posts

3 comments

  1. It sounds like you are talking about this page display template:

    page-{slug}.php – If the page slug is recent-news, WordPress will look
    to use page-recent-news.php

    And you want single-{cpt-slug}-{slug}.php.

    If so, I believe you can approximate that page template handling with the following:

    function cpt_slug_template_wpse_117630($template) {
      global $post;
    
      $templ = locate_template('single-'.$post->post_type.'-'.$post->post_name.'.php');
      if (
        'book' == $post->post_type 
        && 'poem' == $post->post_name
        &&  !empty($templ)) {
          $template = get_stylesheet_directory().'/single-'.$post->post_type.'-'.$post->post_name.'.php';
      }
    
      return $template;
    }
    add_filter('single_template','cpt_slug_template_wpse_117630');
    
  2. Custom post type can have template options like Pages, but its little complex, but i will try to explain it:

    1) you need to register a new meta box with a drop down (custom meta field) of all the templates:

    function register_post_cookingrecipes() {
        add_meta_box('cookingrecipes-details', __('Template Details'), 'add_cookingrecipes_meta_box', 'cookingrecipes', 'side', 'high');
    
        if (('delete_posts'))
            add_action('delete_post', 'delete_meta_cookingrecipes', 10);
    }
    
    add_action('admin_init', 'register_post_cookingrecipes', 1);
    
    function add_cookingrecipes_meta_box($post) {
            $page_template = get_post_meta($post->ID, '_wp_page_template', TRUE);
            ?>
            <label for="page_template"><?php _e('Template') ?>: </label><br/> 
            <select style="width:100%" name="page_template" id="page_template">
                <option value='single-cookingrecipes.php'><?php _e('Default'); ?></option>
                <option  <?php if ($page_template === 'single-cookingrecipes-fullwidth.php') echo 'selected="selected"'; ?> value='single-cookingrecipes-fullwidth.php'>full width</option>
            </select>   
    
         <?php
     }
    

    2) Save/Delete the content of the new meta field when the post saved.

    function save_cookingrecipes_meta($post_id) {
        $post = $_POST['post_type'];
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return;
        if (!current_user_can('edit_page', $post_id))
            return;
    
        switch ($post) {
            case 'cookingrecipes':
                $page_template = $_POST['page_template'];
                if (!empty($page_template))
                    update_post_meta($post_id, '_wp_page_template', $page_template);
                break;
    
            default:
                return;
        }
    }
    
    add_action('save_post', 'save_cookingrecipes_meta');
    
    
    function delete_meta_cookingrecipes($post_id) {
        $post = $_POST['post_type'];
        switch ($post) {
            case 'cookingrecipes':
                delete_post_meta($post_id, '_wp_page_template');
                break;
            default:
                return;
        }
    }
    

    3) The key part for loading the custom template you created is here

    function cookingrecipes_template_redirect() {
        global $post;
        $queried_post_type = get_query_var('post_type');
        if (is_single() && 'cookingrecipes' == $queried_post_type) {
            $page_template = get_post_meta($post->ID, '_wp_page_template', TRUE);
            if ($page_template !== '' && isset($page_template)) {
                include(get_stylesheet_directory() . '/' . $page_template);
                exit;
            }
        }
    }
    add_action("template_redirect", 'cookingrecipes_template_redirect');
    

    any issues , let me know

  3. Suppose you make a new page call art.php

        <?php
    /*
    Template Name: Art
    */
     ?>
    

    Next step you should do is to Go to your admin dashboard. Then follow the simple steps.

    1. Go to Pages
    2. At the right hand side there is “Page Attributes” Template. By default it has “Default Template”. Click it.
    3. Select Art.

    That’s it.

Comments are closed.