我可以为自定义帖子类型分配模板吗?
-
-
http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/((这是帖子,但您可以针对CPT进行修改))http://www.nathanrice.net/blog/wordpress-single-post-templates/(这是帖子,但您可以针对CPT进行修改)这实际上是一个好主意.http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/ ( this is for posts but you can modify it for CPT's) http://www.nathanrice.net/blog/wordpress-single-post-templates/ ( this is for posts but you can modify it for CPT's) It's a good idea for a plugin actually.
- 0
- 2011-07-22
- Wyck
-
8 个回答
- 投票数
-
- 2017-04-23
通过 WordPress 4.7版,您现在可以将自定义页面模板分配给其他帖子类型以及页面.
除模板名称文件头之外,要实现此目的,还可以使用"模板发布类型"来指定模板支持的发布类型:
<?php /* Template Name: Full-width page layout Template Post Type: post, page, product */
您可以在以下页面上获取有关它的更多信息.
https://wptavern.com/wordpress-4-7-brings-custom-page-template-functionity-all-post-types https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
From WordPress version 4.7 you can now assign custom page templates to other post types along with page.
To achieve this in addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: as following.
<?php /* Template Name: Full-width page layout Template Post Type: post, page, product */
You can get more information about it on the following pages.
https://wptavern.com/wordpress-4-7-brings-custom-page-template-functionality-to-all-post-types https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
-
-
谢谢,但是我想知道的是是否可以将自定义模板添加到自定义帖子类型.例如,我可以创建两个模板并将每个模板分配到其各自的职位吗?据我所知,这仅允许指定一个模板文件来处理该特定帖子类型.Thanks for that, but what I would like to know is if it is possible to add custom templates to custom post types. So for instance can I create two templates and assign each template to it's respective post? That only allows for specifying one template file to handle that specific post type as far as I understand it.
- 2
- 2011-07-21
- Odyss3us
-
可以看到,如果单个帖子需要不同的模板,则可能需要根据每个所需的模板创建几种自定义帖子类型.我猜这取决于您需要多少个不同的模板.您将在每个帖子中需要不同的模板中做什么?Is see, if you need different templates for single posts, you may want to create several custom post types, according to each needed template. I guess that depends on how many different templates you need. what are you going to do in the templates that needs to be different on each post ?
- 0
- 2011-07-22
- mike23
-
该答案现在已过期.参见Vinod Dalvi的答案.This answer is now out of date. See Vinod Dalvi's answer.
- 0
- 2017-09-12
- Simon East
-
它不是过时的.仍然可以正常工作,并且仍然是标准做法.It is not out of date. Still works just fine and is still standard practice.
- 1
- 2020-03-01
- Tim Hallman
-
-
- 2015-06-11
这对我有用:
add_filter('single_template', function($original){ global $post; $post_name = $post->post_name; $post_type = $post->post_type; $base_name = 'single-' . $post_type . '-' . $post_name . '.php'; $template = locate_template($base_name); if ($template && ! empty($template)) return $template; return $original; });
因此,给定了一个自定义帖子类型
foobar
的帖子和一条hello-world
的代码,上面的代码将加载single-foobar-hello-world.php
模板(如果存在).Here's what works for me:
add_filter('single_template', function($original){ global $post; $post_name = $post->post_name; $post_type = $post->post_type; $base_name = 'single-' . $post_type . '-' . $post_name . '.php'; $template = locate_template($base_name); if ($template && ! empty($template)) return $template; return $original; });
Thus given a post of custom post type
foobar
and a slug ofhello-world
the above code would load thesingle-foobar-hello-world.php
template, if it existed. -
- 2017-03-08
对于那些通过Google达到此主题的人,WP 4.7引入了适用于所有帖子类型的模板.参见设为WP核心获得完整的演练.您不再受限于所有CPT的一个模板,可以像处理Pages一样逐个分配单个模板.
For those reaching this thread through Google, WP 4.7 introduced templates for all post types. See Make WP Core for a full walkthrough. You're no longer restricted to one template for all of your CPT, you can assign individual templates post by post just like you can do with Pages.
-
- 2016-07-13
这有点旧,但是您也可以尝试以下方法:
为自定义帖子类型创建模板:
single-*custom-post-type-slug*.php
该文件应检查该文件,并验证文件是否存在,如果不存在,则回退到默认模板文件:
<?php $slug = get_post_field( 'post_name', get_post() ); $slug = ( locate_template( 'templates/*custom-post-type-slug*/' . $slug . '.php' ) ) ? $slug : 'default'; get_template_part( 'templates/*custom-post-type-slug*/' . $slug ); ?>
将 custom-post-type-slug 的所有实例替换为您的自定义帖子类型的实际标签.
我这样做是为了易于使用和组织目的.在我看来,比将所有文件都放在主题的根文件夹中更干净.
示例文件夹结构:
themeroot | |single-cases.php |-templates | --cases | |default.php | |case-one.php | |case-two.php
This is a little old but you can also try this:
Create a template for the custom post type:
single-*custom-post-type-slug*.php
The file should check the slug and verify if a file exists, if not, fallback to a default template file:
<?php $slug = get_post_field( 'post_name', get_post() ); $slug = ( locate_template( 'templates/*custom-post-type-slug*/' . $slug . '.php' ) ) ? $slug : 'default'; get_template_part( 'templates/*custom-post-type-slug*/' . $slug ); ?>
Replace all instances of custom-post-type-slug with the actual slug of your custom post type.
I do this for ease of use and organizational purposes. It seems cleaner to me than having all of the files in the theme's root folder.
example folder structure:
themeroot | |single-cases.php |-templates | --cases | |default.php | |case-one.php | |case-two.php
-
- 2015-07-13
首先在页面上创建一个名为Items的页面,然后显示一个模板文件并命名该模板项目.为您创建的页面选择该模板.
<div class="container"> <div class="row"> <div class="col-md-9"> <div class="panel panel-default text-center"> <?php $loop = new WP_Query( array( 'post_type' => 'items', 'posts_per_page' => 5 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_title();?> <?php if(has_post_thumbnail() ) { the_post_thumbnail(); } ?> <?php the_content();?> <?php endwhile; ?> <?php wp_reset_query(); ?> </div> </div> </div> </div>
First create on page named as Items as your wish which display the content from items post types, than create one template file as below and named that template-items. Select that template for the page you have created.
<div class="container"> <div class="row"> <div class="col-md-9"> <div class="panel panel-default text-center"> <?php $loop = new WP_Query( array( 'post_type' => 'items', 'posts_per_page' => 5 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_title();?> <?php if(has_post_thumbnail() ) { the_post_thumbnail(); } ?> <?php the_content();?> <?php endwhile; ?> <?php wp_reset_query(); ?> </div> </div> </div> </div>
-
- 2019-06-07
这很简单.
在主题根目录中创建一个新的PHP文件,并将其添加到顶部:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?>
完整示例如下:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?> <?php get_header();?> <div class="container pt-5 pb-5"> <?php if (has_post_thumbnail()):?> <img src="<?php the_post_thumbnail_url('largest');?>" class="img-fluid"> <?php endif;?> <?php if (have_posts()) : while (have_posts()) : the_post();?> <?php the_content();?> <?php endwhile; endif;?> </div> <?php get_footer();?>
This is very simple to do.
Create a new PHP file in your theme root directory and add this to the top:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?>
Full example will be as following:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?> <?php get_header();?> <div class="container pt-5 pb-5"> <?php if (has_post_thumbnail()):?> <img src="<?php the_post_thumbnail_url('largest');?>" class="img-fluid"> <?php endif;?> <?php if (have_posts()) : while (have_posts()) : the_post();?> <?php the_content();?> <?php endwhile; endif;?> </div> <?php get_footer();?>
-
我可以将模板文件分配给自定义帖子类型吗?
我创建了一个名为
items
的自定义帖子类型,我想像为页面一样为这些项目分配模板.