1

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...

  • 1
    Possible duplicate of Assign Page Template Within A Custom Post Type – Tim Malone Jul 6 '16 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 Jul 6 '16 at 23:29
  • 1
    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 Jul 6 '16 at 23:56
1

From what I understand, you've got two options.

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 it's 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.

Option 2 Example:

single-file.php (as your default template)

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

  • 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 Jul 8 '16 at 23:03
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' );
0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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