如何在自定义帖子类型的页面属性中获取模板下拉菜单?
4 个回答
- 投票数
-
- 2011-12-01
您不能以这种方式将模板应用于自定义帖子类型.仅在帖子类型为"页面"时才会显示(请检查wp-admin/includes/meta-boxes.php第568行).但是,如果您希望以相同的方式设置所有单个自定义帖子类型的样式,但不同于其他帖子类型,则可以使用single-[[posttype] .php-> http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
You can't apply templates to custom post types in this manner. That will show up only if the post type is 'page' ( Check the wp-admin/includes/meta-boxes.php line 568 ). However if you want to style all your single custom post types in the same manner but different from other post types you could use the single-[posttype].php -> http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
-
是的,谢谢.很遗憾,他们还没有此功能.Yeah, thanks. It's too bad they don't have this feature yet.
- 0
- 2012-02-06
- rpeg
-
从4.1版开始,自定义帖子类型是否仍不支持模板下拉菜单?Is the template dropdown still not supported in custom post types as of 4.1?
- 0
- 2015-02-12
- supertrue
-
您/任何人是否找到解决方案以显示CUSTOM POST TYPE的页面属性/模板选择?did u / anyone found the solution to show the page attribute / template selection for CUSTOM POST TYPE??
- 0
- 2015-03-27
- Riffaz Starr
-
- 2017-05-04
自Wordpress 4.7起,自定义模板也可用于自定义帖子类型,在定义模板时,在模板名称下方添加另一行,例如(其中'product'是自定义帖子类型):
<?php /* Template Name: My custom layout Template Post Type: post, page, product */ // your code here
,并记住在注册自定义帖子类型时添加"页面属性":
'supports' => array('title', 'page-attributes'),
显示"发布属性"框.
Well, as of Wordpress 4.7 custom templates are also available to custom post types, when defining a template, below the name of the template add another line like (where 'product' is your custom post type):
<?php /* Template Name: My custom layout Template Post Type: post, page, product */ // your code here
and remember to add 'page-attributes' when registering your custom post type:
'supports' => array('title', 'page-attributes'),
to display the "Post attributes" box.
-
搞定了!正是我需要的.nailed it! exactly what I needed.
- 0
- 2018-12-10
- Marty McGee
-
此处的更多信息:[https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/](https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/)More info on this here: [https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/](https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/)
- 0
- 2020-05-26
- Dvaeer
-
对此一无所知.谢谢!Had no idea about this. Thanks!
- 0
- 2020-07-06
- Keith Donegan
-
- 2017-08-30
以我的主题,我提供"虚拟"模板.我的主题中没有特定的
{template}.php
文件,因此我像这样过滤PAGE模板:function my_virtual_templates( $templates ) { $my_virtual_templates = array( 'virtual_template_id_1' => 'Template 1', 'virtual_template_id_2' => 'Template 2', 'virtual_template_id_3' => 'Template 3' ); // Merge with any templates already available $templates = array_merge( $templates, $my_virtual_templates ); return $templates; } add_filter( 'theme_page_templates', 'my_virtual_templates' );
当我遇到这篇文章时,我正在寻找一种"简单"的方法来在"自定义帖子类型"(CPT)上添加实际的帖子元框.由于我的新CPT将使用相同的"虚拟"模板数组,因此我只需要在适当的位置放置一个postmeta框即可.
使用theme _ {$post_type} _templates 会自动创建此帖子元箱区对我来说.因此,在我的CPT称为
my_cpt
的地方,我像这样添加了过滤器:add_filter( 'theme_my_cpt_templates', 'my_virtual_templates');
现在,出现了meta框和选择器,由于它是内置的,我什至可以在批量编辑屏幕上进行更改.非常方便!
With my theme, I provide "virtual" templates. There are no specific
{template}.php
files in my theme, so I filtered the PAGE templates like so:function my_virtual_templates( $templates ) { $my_virtual_templates = array( 'virtual_template_id_1' => 'Template 1', 'virtual_template_id_2' => 'Template 2', 'virtual_template_id_3' => 'Template 3' ); // Merge with any templates already available $templates = array_merge( $templates, $my_virtual_templates ); return $templates; } add_filter( 'theme_page_templates', 'my_virtual_templates' );
I was looking for a "simple" way to add the actual post meta box on a Custom Post Type (CPT) when I came across this post. Since my new CPT will use this same array of "virtual" templates, I just needed to get a post meta box in place.
Using the theme_{$post_type}_templates It automatically creates this post meta box area for me. So where my CPT is called
my_cpt
I added the filter like so:add_filter( 'theme_my_cpt_templates', 'my_virtual_templates');
Now, the meta box and selector shows up, and I can even change on the bulk edit screen since this is all built in. Very handy!
-
- 2018-10-23
只需创建任何模板文件并在此模板的标头中设置
:/* Template Name: Some Name Template Post Type: your_type, page */
然后模板选择器出现在"发布属性"中
just create any template file and set in header of template this:
/* Template Name: Some Name Template Post Type: your_type, page */
then template selector appears in 'Post Attributes'
注册我的自定义帖子类型时,请进行以下设置:
因此,我很想在创建新帖子时在"属性"框中看到"顺序","模板","父母".但是,我看不到"模板"下拉列表出现. 为了启用"模板"的选择,我应该做些其他事情吗?