Archived topic
Custom post type template
7 replies · Started by Fatih on November 18, 2016
Hi, I'm trying to create a template for a custom post type. I have created the file single-posttype.php. and I have noticed that it also uses the content.php. How can I create content-posttype.php and make single-posttype.php use it? I know this sounds complicated but I hope I made it clear.
Thanks
In your file, find:
<?php get_template_part( 'content', 'single' ); ?>
Now if your file is named content-posttype.php, replace it with:
<?php get_template_part( 'content', 'posttype' ); ?>
Awesome, thank you so much :)
You're welcome :)
Hello Tom!
This information is very useful, thanks!
I'm also working with custom post types and I want to edit not only the page template but also the archive page. So in my child theme I've created single-myCPT.php where I wrote
<?php get_template_part( 'content', 'single-myCPT' ); ?>
I've also created archive-myCPT.php but I don't know how to reference my custom content replacing get_post_format().
<?php get_template_part( 'content', get_post_format() ); ?>
Hope I explained well...
Thanks in advance!
This line: get_template_part( 'content', 'single-myCPT' )
Will look for a file like this: content-single-myCPT.php
If you're already using a custom single-myCPT.php file, you might as well copy the code inside content-single.php, and replace this with it:
get_template_part( 'content', 'single' )
get_template_part() is cool as it allows you to only change the loop - not the entire single.php file. In your case, it's probably easier to keep everything in one file.
Dear Tom,
If i want to create a custom page, which page of your theme will be most usefull to get a basis ?
content.php, page.php ??
It depends on which areas you're editing. page.php is likely the one you want to edit.