admin管理员组文章数量:1200985
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...
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 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
Reset to default 0From 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
*/
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
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
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' );
本文标签: Add a Template to a custom post type
版权声明:本文标题:Add a Template to a custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738542706a2095809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论