Using Custom Post Types to Create a Killer Portfolio

Quite possibly the best addition to WordPress 3.0 was that of Custom Post Types. This took WordPress from being a CMS that can manage posts and pages to being able to manage anything the user can think of rather easily. You no longer have to add custom fields to posts- you can add high level support to your own types, creating their own theme page files and admin areas. One of the first things I did using custom post types was revamp my portfolio and today I'm going to show you how I did it!

We recently showed you a few neat tools for creating "Instant" Custom Post Types. Today is all about actually building a project using CPT's from scratch... We'll be doing everything from creating the custom type - to styling it for use in the theme. The CSS (and CSS3!) I use should be generic enough for most themes, but in case it isn't, feel free to change it! This is intended to be used as a launchpoint for everyone, so feel free to get creative!


Getting Started A Few Thoughts

The first thing to note is that when creating a Custom Post Type, we have a couple of options as to how we want to approach them; The two main implementations you might consider are:

  1. As part of the current theme (usually through the functions.php file)
  2. As it's own stand-along plugin.

There are pros and cons to each. In this tutorial, since we are integrating with our theme, we'll make it part of the theme, calling everything we need through the functions.php file.

Developer's Note: Because we'll be building this directly into our theme, that doesn't mean that this is the right way for you though... consider the end-use of your theme. If there's a good chance that your users are going to be switching themes in the future, or you're releasing your theme as a public or premium product for lots of people to use, you'll probably want to build in your custom post type as a standalone file that people can take with them without too much digging through your code.

Just think of it this way, if your theme is the only thing that's loading your Custom Post Types and you change themes, the Custom Post Type data won't be usable; It'll still exist on the database, but it won't show up in new themes in any meaningful way. Sometimes this isn't possible if you're really overhauling a theme to use a CPT by including lots of customizations and templates, but at least consider what users might need to do to preserve the use of their data in the long run.

Let's get started then!


What we'll be creating

Step 1 Set-Up

As I stated above, we'll be adding this to our current theme (I'm using my own custom theme), so the first thing we need to do is go to our theme and open up the functions.php file. We're going to put our custom post type code in a different file (just so it's easier to read/manage), so we'll call that file at the top of our functions file:

Now we're going to add two files to our theme: portfolio-type.php and portfolio.css. As you can probably guess, all of our CSS for the new type will go in the latter file.


Step 2 Registering the New Type

Thumbnail & Featured Image Support

Before we register the new type, we need to add support for an integral part of the portfolio presentation- featured images. After adding the opening and closing php tags to portfolio-type.php, add the following code:

These lines, after checking to make sure your particular install supports post thumbnails, will add them to your current theme, then set a couple of default sizes. set_post_thumbnail_size() will, as the name suggests, set the default size for the thumbnail. The next line (add_image_size() will create another image that we can call named 'screen-shot', which will be 720x540. We will be using these images when displaying our portfolio.

Creating a New Post Type

Right here is where the magic happens- we will now tell WordPress about our new post type. Add the following code to portfolio-type.php:

The first line here is a hook in WordPress that will call our function, portfolio_register() on initialization. The function itself sets up an array of arguments to send with our custom post type. Most notably, we're setting our admin labels, giving this type all of the capabilities of a standard WordPress post, allowing URL rewrites (for pretty permalinks), and adding support for the title, editor, and featured image fields. You can read more about all of the arguments for register_post_type() here.

After setting up the arguments array ($args), we pass that along with the type name to the function register_post_type().

Adding a Custom Taxonomy

The last thing we'll do in this section is create a custom taxonomy for our new type. Add the following line of code to your portfolio-type.php file:

This will create the new taxonomy 'project-type' and apply it to the post type 'portfolio'. You can read more about register_taxonomy() here.


Step 3 Adding Custom Fields

Creating the Custom Field Box

We wouldn't have much of a custom type without having some extra info to add to the post. We'll add that info in the form of custom fields. Specifically, we'll be adding one extra field for a link to more info about the project, or to the project itself. Add the following code to your portfolio-type.php file:


This code will create the "Project Options" box we see here.

First we use the WordPress hook admin_init to call our function portfolio_meta_box() when the WordPress admin is created. Our function will add another box to our portfolio type, which can be populated with anything. What the box is populated with is covered by the 3rd argument, which is a callback function. In this case, our function is named portfolio_meta_options().

In portfolio_meta_options() we'll create a form field that will be used to capture the project's link. The first thing we do is grab the global $post array so we can get the custom fields for whatever post we're editing. In our next line, we're checking to make sure WordPress isn't currently saving the post or custom fields; if it is, we may see inaccurate results when we do grab the custom data.

If WordPress is not doing a save, we grab the custom fields for the current post and create a form field using that info. $custom (what's returned from get_post_custom() is a 2D array where the key is whatever we name the form field for our custom in. Take a look at our text box for the link. You'll notice that the name matches the index we call in our $custom array. You'll also notice that we don't have a separate form or submit button. This field is added to the form used to edit the entire post.

While we only create one here, you can obviously create as many as you'd like.

Saving the Custom Data

Now that we've created our custom meta box, it's time to create a function that will save the information. Add the following lines of code to your portfolio-type.php file:

First, as usual, we have our hook- this time to call our function when the post is saved. In the function itself, we again grab the $post array so we can get the post ID and check to see if the post is autosaving. If we don't include this line, we will lose our data, so it's important we keep that in.

If the post is not updating, we save our custom fields using update_post_meta(), sending the post id, the name of the custom field, and the new value.


Here's what the whole kit and caboodle looks like!

Step 4 Customizing Admin Columns

Here we're going to customize the list displaying all of our projects. Add the following code to your portfolio-type.php file:


Here's our newly modified Project list.

After our hook for the first function (project_edit_columns()), we do something pretty interesting in terms of editing the columns. WordPress places the columns for a post type's display list into an array with a key and a value. We create those key-value pairs in this function. In the next function (project_custom_columns()), we use a switch statement to grab the key and then display the information we want based on it. As you can see, just like in the previous section, we get the global $post array so we can get the post's ID to properly display any custom information, like our link and taxonomy.

You've probably noticed that the number of cases does not match the number of columns in our $columns array. That's because for certain keys, like cb and title, WordPress has default values that we don't want to overwrite.


Step 5 Adding Some Display Functions

Before we get to creating a template page, I want to give you some functions I included to get displaying the projects just right. Open up your functions.php file and add the following three components:

The first two hook/function pairs are common to WordPress- they simply change the length of the excerpt and the "more text" indicator, which I've replaced with just a space. Please note that this will happen to all posts, not just the portfolio post, and that if you're using a child theme, this may not work.

The custom function, portfolio_thumbnail_url() takes in a post's ID as the argument and grabs the screen-shot version of the image we uploaded. We'll be calling it in our template page, so keep an eye out for it!


Step 6 Create a Template Page

Now that we have our custom post type all set up in the admin, it's time to create a theme page so we can display them. To accomplish this, we're going to create a template with the name, "Portfolio" and then assign the template to a page in WordPress. While we can also create archives-portfolio.php to accomplish the same thing, we won't be able to add that page to a menu from the WordPress admin, so this route is best. Keeping with WordPress naming conventions, create a new file called page-portfolio.php and add the following code:


This will create the 'Portfolio' template.

These few lines establish the most important parts of the page. The first line lets WordPress know that this is a page template that should go by the name "Portfolio." Then, after calling the theme's header, we set up the query to grab the last 9 posts of type portfolio. The next thing to do is display them. Add this code to your page-portfolio.php file:

You'll notice a few things here: first, after we're in the loop, we need to trim extraneous '"' characters from our title and content using str_ireplace(). I'm not exactly sure why they appear here, but from what I can tell this is a necessary step. You should also notice that we're using lightbox. While we could add it ourselves (which I'd probably recommend if this were a plugin), since we are modifying our own theme, we could download one of the many lightbox plugins available in the WordPress repository. Just pick your favorite!

Aside from that, this should look familiar to anyway who's worked with the WordPress loop. What we're doing here is creating blocks with our thumbnails and descriptions, which link to (using lightbox) the screen shot for each of the 9 projects. I didn't include links to other pages (in case you have more than 9 projects) because I only want my users to see latest 9 projects. You can allow users to get to other posts using posts_nav_link(), if you so choose.

I should also note that I removed WordPress's default more link because it linked to a single post page, and I wanted to use lightbox, so I constructed my own.


Step 7 Styling our Portfolio Type

Here's the fun part: making our portfolio type look pretty. Here's some CSS that I've included- you can add it to the bottom of your style.css sheet, or into our newly created portfolio.css file. Just be sure to use @import to call portfolio.css at the top your theme's style.css (it will not work anywhere else):

If you'll reference our template page, you'll see that each project is wrapped in a div called "item," which we now apply CSS to. Since no two themes are the same, you may have to tweak your own CSS a bit, but here is a nice starting point for you. I've also included a class for self-clearing floats. This is a technique I got from Dan Cederholm, and I think it's a bit more elegant that the standard "clearfix" method of clearing content after floating divs.


Conclusion

That's it! You now have a simple portfolio using custom post types. While this was built to serve my needs, and I wanted to keep things simple, the possibilities for this are endless as you can tailor it to fit whatever kind of portfolio you have (writing/articles, photography, graphic design, etc.). If you don't want to use lightbox to display the single project, you can also create a theme page with the name single-portfolio.php, which will then apply the code within to each individual project, much like single.php does for blog posts.

Tags:

Comments

Related Articles