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 How to Make a Website with WordPress Custom Post Types and Fields in WordPress Custom Post Type Templates

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

Why use page.php for your template rather than archive.php?

I have some limited knowledge of custom post types. What I have traditionally seen, in terms of displaying them, is that people take the archive.php file customize it, and then name it archive-$posttype.php. However, in this lesson Zac uses the page.php file.

I'm just wondering when you would want to use the archive.php rather than the page.php and vis versa?

Additionally, He names the file art.php, shouldn't this be page-art.php? art.php is not in the template hierarchy, so how does WordPress know to look for this file?

2 Answers

Matthew Leung
Matthew Leung
1,434 Points

You could use either. They both have similar code. But archive.php is for listing all the old posts.

https://codex.wordpress.org/Creating_an_Archive_Index

If you create a custom page template, you can name the file anything you like (eg. art.php, template-art.php). You'll just have to add some comments at the top of the page so that WordPress knows it's a page template (https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/#creating-custom-page-templates-for-global-use). When you create a new page via the WordPress admin area, you'll assign this page template to your new page.

Generally, you'll want to use archive.php (or archive-CUSTOM-POST-TYPE.php) when you want to list out your posts in an archive fashion (think search results and blog posts, but it could be anything). On the other hand, page.php or art.php or full-width.php is to display a certain type of content and/or layout.

Hope this helps!