最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I assign a template to a custom post type?

programmeradmin2浏览0评论

Can I assign a template file to a custom post type?

I have created a custom post type called items, and I would like to assign templates to the items like you can do with pages.

Can I assign a template file to a custom post type?

I have created a custom post type called items, and I would like to assign templates to the items like you can do with pages.

Share Improve this question edited Sep 22, 2016 at 3:18 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Jul 21, 2011 at 15:38 Odyss3usOdyss3us 9051 gold badge12 silver badges18 bronze badges 1
  • wpbeginner/wp-themes/… ( this is for posts but you can modify it for CPT's) nathanrice/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. – Wyck Commented Jul 22, 2011 at 1:43
Add a comment  | 

8 Answers 8

Reset to default 96

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/wordpress-4-7-brings-custom-page-template-functionality-to-all-post-types https://make.wordpress/core/2016/11/03/post-type-templates-in-4-7/

You can create template for custom post types by creating files, like for example :

single-mycustomposttype.php

See Template Hierarchy in the codex.

PS : this has already been answered.

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 of hello-world the above code would load the single-foobar-hello-world.php template, if it existed.

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.

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

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();?>

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>

We worked out another solution using the custom post template plugin

发布评论

评论列表(0)

  1. 暂无评论