Better Practice, Dec. 2018

(singke) #1
http://www.phparch.com \ December 2018 \ 9

Custom Post Types in WordPress

Using the Hiking example custom post types of mycpt_hike,
we would create files for single-mycpt_hike.php and archive-
wiverb_hike.php.
I often copy and rename the index.php and archive.php files
from my theme to begin templating these pages. This can also
likely be copied from a parent theme if you are working with
a child theme. From here, you can specify the HTML for your
custom post type and add unique styles to meet the needs of
your layout.
If your goal is to display one custom post type per page or
per archive page, you can utilize the same WordPress loop
from the default page. You can continue to use templating
functions such as next_posts_link like you would for the
layout of a blog post.
Once you’re working on theme pages that are specific to
your custom post type, you can also access functions like
get_post_type_archive to link to the archive/listing page for
your custom post type.

Querying Custom Post Types In a Page
If you want to display data from your custom post types on
a WordPress page, you can modify the main query to include
your custom post types:

function add_wiverb_cpt( $query ) {
if (is_home() && $query->is_main_query()) {
$query->set('post_type',
['post', 'page', 'wiverb_hike']);
}
return $query;
}
add_action( 'pre_get_posts', 'add_wiverb_cpt' );

Learn more about Post Types^11
You can also create a second WP_Query call if your page
requires multiple queries. This is handy when you want to
display a collection of custom post type entries on a page with
an intro. You can create a custom page template that queries
your desired custom post types to do this.
Check out Listing 6 for an example of a custom page
template designed to display two custom post types. For this
example, we want to show a Trails page displaying both hiking
and biking trails. The query is set to limit six items per page

11 Post Types: https://codex.wordpress.org/Post_Types


and to order these items by their title A-Z. Then, depending
on the post type, we can build in some logic to switch up the
text and add a CSS class to the resulting HTML to help us add
some style to differentiate between the two post types.
Learn more from the WordPress Documentation on Multi-
ple Loops^12.

Designing WordPress Websites with
Custom Post Types In Mind
When designing a new site, you should keep the following
in mind to see where you can best use custom post types:What
design elements do we see repeating?


  • What content will need its own page?

  • Is there content that will need to be re-sorted or
    searched?

  • Is there content that will need to be syndicated or
    displayed on other websites, RSS feeds, or apps?


Content-First Accelerates Design And Development
You can create a map of your content types and the fields
they will include as an early deliverable for your client or
team to help determine which data makes the most sense for
your website and how best to build for that data.
With custom post types, you can get better leverage the
power of WordPress for many kinds of content.

Andrea Roenning is a Senior Web
Developer and Designer at Forte in Madison,
WI where she develops and maintains
several WordPress sites. She has been
building websites since 2001. Andrea is also
a co-founder of Codecinella, a Madison,
Wisconsin non-profit with a mission to
empower women software engineers. She
tweets about tech, music and motherhood at
@andreaincode.

12 Multiple Loops: https://phpa.me/wp-multiple-loops

Related Reading


Free download pdf