Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

valeriuv
valeriuv
20,999 Points

Creating custom templates for custom post types

Hi!

I have a theme with custom post types. There is a page template for those custom post types called single-{custom-post-type}.php. I was wondering if there is a way for me to create an alternative template, maybe called custom-{custom-post-type}.php, which I could select from the dropdown menu in Page Attributes box.

I have enabled support for 'page-attributes' in the file that registers the custom post type. However, there is no dropdown for the template.

Any suggestions?

Thanks!

4 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

It should be as simple as saving a copy of the exiting custom post type template which is a PHP file.

Pay attention to the comments at the top of the page. It will contain the template name which is how Wordpress knows the make the template available in your post attributes.

Good luck :)

Hi,

Hope I'm not misunderstanding what you need here. You can add a template to the standard Template drop down list in the Page Attributes section by using the following comment at the top of your file:

<?php /* Template Name: Example Template */ ?>

More information can be found on the WordPress Theme Developer Handbook, particularly the section named Creating Custom Page Templates for Global Use.

Hope that helps :)

-Rich

EDIT: Sorry I had misread the question. I don't think this is a standard feature but you may be able to use something like the WP Custom Post Templates plugin to achieve this.

Please note I have not used this plugin before so I'm not 100% sure of how good it is. There may be other plugins that do a similar thing so I'd recommend researching these first.

valeriuv
valeriuv
20,999 Points

Jonathan, Rich, thank you for your answers.

However, I made a copy of the single-{custom-post-type}.php file, named it custom-{custom-post-type}.php and added the Template Name comment as suggested, however that did not bring the dropdown menu under Page Attributes.

That registers it as a PAGE template, not a custom post type template. It does appear under the Template dropdown when I want to create a new page, but not when I create a new instance of the custom post type.

Just updated my answer :)

-Rich

valeriuv
valeriuv
20,999 Points

@Rich Bagley Just installed it, still no dropdown.

Thanks anyway!

Hi,

Can you post your code for one of the created templates please?

Thanks

-Rich