3

I'm recoding my website with custom post types to be better organized, have a better search experience and being able to hide some pages from other contributors, but I have a little problem.

I've set up a post type named Files which contains some files that my contributors should not see. Basically the legal pages, the contact page and so on.

As I had all the pages in the default Pages post type I was able to select a template for each page in the page attributes. Now in my new custom page type, I can only select the order of the page and nothing else. So I have a template for the legal pages and another one for the contact page, which includes all the PHP code. How can I select these templates in my new custom post type?


EDIT

Okay, I've understood, that it's only possible to set a template by setting up a PHP file named single-*(post_type_name)*. But as I said, I've two different templates, and more will come very soon, so how can I set these to one or maybe two posts inside that post_type. There must be a possibility, isn't it? The makers of WordPress will unlikely have us create a new post_type for a single file...

3
  • 1
    Possible duplicate of Assign Page Template Within A Custom Post Type
    – Tim Malone
    Commented Jul 6, 2016 at 23:28
  • 3
    ^ While that duplicate essentially asks the same question and has an answer, here's another and another which provide a simpler way to do what you're asking. Found from a quick search - please do search before asking :)
    – Tim Malone
    Commented Jul 6, 2016 at 23:29
  • 2
    I saw these questions but I think that I'm searching for something else, or I simply don't understand the given answers correctly... I want the same dropdown menu for my custom post types as the default one for the pages. I do not want to hardcode the page template to that post type because I need different templates for the different pages inside the custom post type.
    – Sam
    Commented Jul 6, 2016 at 23:56

4 Answers 4

0

From what I understand, you've got THREE options (updated).

Option 1: Dynamic Solution Create categories for your custom post type - each category is going to have its own template.

You then create a single template that splits off based on category. Meaning you use the general header and footer in your single-postypename.php and anything else that you want to apply to both templates, but in the meat of it, you then create some php logic for "if category x, use content y template (or partial, I like partials)" and "if category z, use content z template (or partial)". I assume that if you're working in templates you're okay with the code for that, but if not just comment and I can put together an example.

Option 2: Static Solution Each post inside your custom post type gets its own template.

You need the single-posttypename.php as your default, but then you can create single-postypename-postslug.php and presto, you have a custom template for that specific post that you can mess around in. As long as your slugs match, it'll just know what to do.

Example:

single-file.php (as your default template)

single-file-legaldocument2.php (as a custom template for yourdomain.ca/file/legaldocument2 )

Option 3: Identify a template

WordPress now offers the ability to assign a template based on post type. I don't think this can get as granular for categories of a post type or a specific post, for that you would still need solution #2.

Example:

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
 
3
  • Awesome!! I really like the second method because I want my files to be organized and while the first option is great too, I think it's kind of messy. Thanks for your help!
    – Sam
    Commented Jul 8, 2016 at 23:03
  • This is no longer the best answer. As stated in another answer, "From WordPress version 4.7 you can now assign custom page templates to other post types along with page. Please see the answer posted in this topic wordpress.stackexchange.com/a/264573/14347" This answer need to be updated to include that information or the other answer should be updated to be the correct way to handle this.
    – Mav2287
    Commented Jan 4, 2022 at 4:22
  • I've updated, however the question was about using two different templates within the same post type, so I don't feel like this is out of date. The post type template solution is still limited.
    – Faye
    Commented Aug 4, 2022 at 21:51
1

Create a template file like this in your child theme.

<?php

// Template Name: CPT Template
// Template Post Type: files

Assumes files is the name of your custom post type.

WordPress now supports this functionality

1

From WordPress version 4.7 you can now assign custom page templates to other post types along with page. Please see the answer posted in this topic https://wordpress.stackexchange.com/a/264573/14347

0

The single_template filter will let you use whatever template file you want for your CPT, but it's up to you to provide some means of selection and storage of a template name or some other reference to make it a dynamic solution.

The built-in page post type does this by reading all of the valid template files in your theme, and adding that list to a meta box, which stores the selected filename in post meta.

You can quickly duplicate this behavior by saving a filename manually in a Custom Field (post meta), then adding a filter to check for that meta key with the current post. Here's a quick untested example from my other answer linked in a comment above-

function wpa_single_cpt_template( $templates = '' ){
    $single = get_queried_object();

    if( 'files' == $single->post_type ){
        $template_name = get_post_meta( $single->ID, 'my_template_file', 'true' );
        if( !empty( $template_name ) ){
            $templates = locate_template( $template_name, false );
        }
    }
    return $templates;
}
add_filter( 'single_template', 'wpa_single_cpt_template' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.