It turns out to be extremely easy to teach WordPress how to handle more than just blog posts and pages. In this post, I’m going to describe how I converted my hand-coded, static portfolio/CV home page into a WordPress page powered by custom post types. This isn’t a full step-by-step tutorial but rather a medium-level overview of the general approach with some code examples.
Prior to yesterday, my home page was hand-coded outside of WordPress and WordPress was responsible solely for the content in the /blog directory. (I’d created the site a long time ago and decided to add a blog to it later.) Whenever I needed to update or rearrange my portfolio, I’d get the old HTML editor out and move a bunch of markup around. Adding a new entry involved a lot of copy and paste. So I decided to add a custom post type specifically for Projects.
The first step is to register your custom post types. I did this in my theme’s functions.php file:
That code adds a Projects menu which allows you to manage “Projects” in the WordPress admin just like you can manage Posts and Pages. It also excludes those posts from search results and customizes which blocks are visible in the editor. You can read up on all of the options in the Codex.
From the new Projects menu, I can add, update, trash, draft, publish, and unpublish Projects just like I can with Posts. It’s very cool.
The next step was to create a custom page template to show those projects. Here’s the entire template for my home page:
Here are the important bits: Lines 14-19 loads all of posts in the the ‘project’ post type defined earlier. Lines 20-21 setup the loop for the posts. Line 22 allows you to use functions like the_content() within your loop just like any other WordPress template. Lines 49-54 show the content associated with the actual page using this template.
That’s basically it. Register your post types, create a template. Easy as 1, 2.