I recently released a plugin on WordPress.org that registers a portfolio post type, related taxonomies, and adds an image when you’re viewing the items in the dashboard. It clocks in at a neat 202 lines of code, most of that being labels.

I’ve held onto to the plugin for a while in GitHub since it’s an easy thing for any developer to build out- but I’ve recently been thinking that a standard plugin in the repository might be beneficial as it could make more themes more portable. (Especially for users of my Portfolio Press theme)
And since I expect people to modify this a bit as well, I wanted walk through of the code.
Plugin Header
/* Plugin Name: Portfolio Post Type Plugin URI: http://www.wptheming.com Description: Enables a portfolio post type and taxonomies. Version: 0.1 Author: Devin Price Author URI: http://wptheming.com/portfolio-post-type/ License: GPLv2 */
Explanation: All plugins need to have basic header data in order to show up in plugin admin page. If you don’t want to get updates to the plugin (I don’t expect many), change the name to something different. Shoot, feel free to change any of this- that’s why it’s being released!
Enable the Portfolio Post Type
function portfolioposttype() {
/**
* Enable the Portfolio custom post type
* http://codex.wordpress.org/Function_Reference/register_post_type
*/
$labels = array(
'name' => __( 'Portfolio', 'portfolioposttype' ),
'singular_name' => __( 'Portfolio Item', 'portfolioposttype' ),
'add_new' => __( 'Add New Item', 'portfolioposttype' ),
'add_new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
'edit_item' => __( 'Edit Portfolio Item', 'portfolioposttype' ),
'new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
'view_item' => __( 'View Item', 'portfolioposttype' ),
'search_items' => __( 'Search Portfolio', 'portfolioposttype' ),
'not_found' => __( 'No portfolio items found', 'portfolioposttype' ),
'not_found_in_trash' => __( 'No portfolio items found in trash', 'portfolioposttype' )
);
$args = array(
'labels' => $labels,
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
'capability_type' => 'post',
'rewrite' => array("slug" => "portfolio"), // Permalinks format
'menu_position' => 5,
'has_archive' => true
);
register_post_type( 'portfolio', $args);
}
add_action( 'init', 'portfolioposttype' );
This is the basic code needed to register the portfolio post type. I’ve wrapped it all in one function that fires on init. For a deeper explanation of all the arguments ($args) check out the codex post.
Register Taxonomies
Wrapped in that same function I also register two taxonomies for categories and tags. Since that code is long, and basically just a bunch of labels I’ll point you to the actual code on GitHub. If you don’t want taxonomies, you don’t actually need that code.
Use Post Thumbnails
If you want your post type to be able to use thumbnails, you’ll need to register it:
// Allow thumbnails to be used on portfolio post type add_theme_support( 'post-thumbnails', array( 'portfolio' ) );
View Thumbnail Images in Column View of Dashboard
For something like portfolio items, it’s nice to see the images when you’re scanning posts in the dashboard. I wrote about how that functionality works in a separate post. That’s handled by the code listed under:
/** * Add Columns to Portfolio Edit Screen * http://wptheming.com/2010/07/column-edit-pages/ */
Display a Custom Icon for Portfolio Menus
I was lucky enough to get Ben Dunkle (the designer of the WordPress icons), to make some portfolio icons for me. To display theme out in the dashboard, here’s the code:
/**
* Displays the custom post type icon in the dashboard
*/
function portfolioposttype_portfolio_icons() {
?>
<style type="text/css" media="screen">
#menu-posts-portfolio .wp-menu-image {
background: url(<?php echo plugin_dir_url( __FILE__ ); ?>images/portfolio-icon.png) no-repeat 6px 6px !important;
}
#menu-posts-portfolio:hover .wp-menu-image, #menu-posts-portfolio.wp-has-current-submenu .wp-menu-image {
background-position:6px -16px !important;
}
#icon-edit.icon32-posts-portfolio {background: url(<?php echo plugin_dir_url( __FILE__ ); ?>images/portfolio-32x32.png) no-repeat;}
</style>
<?php }
add_action( 'admin_head', 'portfolioposttype_portfolio_icons' );
I also wrote about custom post type icons in more depth on this post.
Custom Meta Boxes
This plugin doesn’t register any custom meta boxes, but those obviously might be useful if you want to include a link a website, or a purchase button, etc. If you’re interested in doing that- see my post on adding custom meta boxes to post types.
Hello.
I used your cool theme, now i downloaded portfolio plugin.
I noticed, that strings with “_x” are ignored, when translated.
for example when I translate:
‘name’ => _x( ‘Portfolio Categories’, ‘portfolioposttype’ )
I still get english version.
It look like one argument is missing, it should be something like:
‘name’ => _x( ‘Portfolio Categories’, ‘CONTEXT’, ‘portfolioposttype’ )
I changed if for now, but i don’t like to hardcode plugins, can you please take a look at it?
Cheers
Jacek
I am a beginning designer, and I am really happy to have found your portfolio custom post type.
it seems to have all the functionality i want for a main aspect of my new website,
i need a searchable, functional catalog of images (10000+) with separate categories and tags.
the problem i am having is that my theme does not have a single.php file, it seems to run through the index,
it doesn’t work if i make a new single (post) template file, the way it does with my page templates.
This codex article explains how the template hierarchy works: http://codex.wordpress.org/Template_Hierarchy
If you don’t have a single.php you can rename index.php and use it instead.
Thanks for this work. It is good to have clear examples to learn from. Good things will come your way.
Hello Devin! I have a problem using your plugin:
I created categories into Portfolio but I can’t call them from the theme. I never find the categories by ID or by the name (category_name = categortname). What need to change the theme using your plugin? Thank you very much!
It’s a custom taxonomy, so it wouldn’t show up like a regular post category. You can read more about it in the codex: http://codex.wordpress.org/Taxonomies
I’ts working! thanks Devin, for all this.
Hello. Thanks for the portfolio theme.
I’m working whit portfolio theme and plugin and it’s awesome.
I need to put thumbnails who replaces the “next-previous page” text in the portfolio posts, but i don’t know if it’s possible.
I would like too to make a page whith all portfolio thumbnails and every thumb linking to his respective portfolio post.
Sorry but my english is very poor.
Thanks a lot.
Hello! I’m using your Portfolio Press theme and have now downloaded your portfolio post plugin. I love both of them! The problem I’m having is that whatever image I use as the featured image to show up on the portfolio page is showing up twice on the single portfolio post page. What do I need to do to stop this from happening? What file is this code in?
Either don’t insert your image into the post, or turn of the automatic display of images in the options panel.
I’m trying to set up this plugin on my new site (not live). I created an archive-portfolio.php file, which is working. But when I open a category page, e.g. ‘/portfolio_category/example’, WordPress is still using the ordinary archive.php file. Why is this? What do I need to change? Do I need a category-portfolio.php file? (And if that’s the case, why is WordPress using archive.php to display portfolio categories instead of category.php?) Thanks!!
Taxonomy pages will default to archive.php unless you are using a more specific taxonomy template (e.g. portfolio_category.php). See the template hierarchy here: http://codex.wordpress.org/Template_Hierarchy.
In Portfolio Press I also wanted the portfolio taxonomy templates to use the portfolio-archive.php, so I wrote a filter to override the default template hierarchy. See function portfoliopress_template_chooser in: https://github.com/devinsays/portfolio-press/blob/master/functions.php
Thanks Devin :)
Devin Good night, all right?
If I want to list the project types that signed up I use the following code
”, ‘taxonomy’ => ‘portfolio-type’, ‘walker’ => new Portfolio_Walker ()));?>
How would I do to Portfolio Categories list, taxonomy portfolio_category?
Thanks in advance.
I think you code got cut off. Can you paste it into pastebin (or somethign) and link? What is the walker for?
Hi I am using your plugin as part of a theme I am developing. I have setup the portfolio type with several categories and child categories. I have a template page – archive-portfolio.php that is properly being called, however when I select a subcategory the query returns all the portfolio items and not just the posts within the sub category. Can you offer a quick snippet of what the query arguments should look like to only display the category that is selected or called ?
Thanks
Suz
You shouldn’t be doing a specific query. You should just use the loop: while ( have_posts() ) : the_post();. Not sure why that wouldn’t be returning the correct items for you.
Thank you for the great plugin
I would like to know if it is possible to rename the portfolio to purchase since I want to use it for my purchase area instead of using it for an portfolio.
Shou.ld I rename the templates to single_purchase and archive_purchase and create and page called purchase.. (I really have no clue how to do this)
Thank in return.
You can probably just open up the plugin and do a straight find/replace of “portfolio” -> “purchase”. And then, yes, you’d need to rename those templates.
Hi Devin,
I am trying to set up a simple xml-rpc post method using python. While I cannot seem to create a portfolio item remotely, I can create a post and convert it to a portfolio item (using Post Type Switcher). Unfortunately the tags that are associated with the post are lost in translation. Is there anyway to retain that part of the post taxonomy and still have access to the other portfolio features? Or perhaps there is a simple way to rpc in my portfolio items? Thanks for the great work!