Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 6 • Advanced Theme Usage 161


THE POWER OF CUSTOM POST TYPES


Custom post types are an extremely powerful tool. Thanks to this feature, you are no longer
restricted to just posts and pages; you can create your own post types tailored to your needs. If
you want a post type that is almost identical to WordPress standard pages (which is a post
type in itself ) but with tags, then you can create a post type that does just that.


Being able to create and name post types that work alongside posts and pages has another
benefit as well: They are very user friendly. The user will instantly know what to do because
the custom post type resides in the left menu in the admin, much like posts and pages, and
working with it works in the same way.


USING CUSTOM POST TYPES


Custom post types come in handy in a lot of cases, but the more you need to divide the
content, the more useful they get. In a lot of ways, custom post types are what really made
WordPress a great CMS option for larger traditional sites. Although everything could be
solved previously with posts and pages and some clever thinking, custom post types make it
so much easier to build sites with lots of different types of content.


Custom post types are not just for that; plugins use them to store data. From feedback plugins
that look like e-mail forms to forum software, custom post types have a lot of uses. Thanks to the
WordPress template system, it is really easy to create interesting content and features in themes
because it is just like working with any other part of a site. One thing worth remembering,
however, is that content from custom post types won’t show up in your theme’s front page
loop, in search results, or the like unless you alter the loop.


You’ll do some fun stuff with custom post types in Chapter 14, “Uncommon WordPress
Usage.” Before that, you’ll look at how you work with custom post types in themes in
the following subsection, and the next chapter will help you create your first custom
post type.


THEMING CUSTOM POST TYPES


Custom post types work much like any other post type when it comes to making them look
and feel the way you want. You use archive-X.php, where X is the slug for the custom post
type (for example, if you have a post type with the slug product, archive-product.php would
be the template file to use), to control the custom post type’s archive, much as you would use
category.php or tag.php to work with category or tag archives. Single custom post type posts
use single-X.php, where X is the slug. Refer to Chapter 4 for a list of which template files are
used under what circumstances.


There are also plugins that will create custom post types, such as the bbPress (http://
bbpress.org) forum plugin. Because these plugins are using custom post types, you can
easily work with their output by using the appropriate template files with your theme. Very
h an dy.

Free download pdf