How To Create A Dynamic Portfolio in WordPress Using Categories
There are plenty of portfolio themes for sale out there, but many just don’t have “it” as far as the certain design element you’re looking for in your portfolio. Sometimes it’s just better to design one yourself. But what if you want to put it in WordPress and make your portfolio a little easier to update?
Right now, I’m working on redoing this whole site to make it a bit cleaner and prettier. The site has exploded in terms of both traffic and content so things are getting a little claustrophic over here. I’m also planning on moving the whole site over to WordPress, because as it is, the portfolio is a major PIA to update right now – I have to manually update the recent section on the homepage AND the portfolio AND the blog sections every time I add a new piece…that’s over 10 images I have to make!
For that reason alone, I thought I’d share with you a great way to make a dynamic portfolio page – that will update your blog, portfolio and homepage recent projects section. You can see how I did this at www.emaginewebmarketing.com where I was tasked with combining 3 different pre-made themes and had to do most of the portfolio section by hand.
Let’s see what we’re looking at on the homepage:
And this is what the “Portfolio” page will look like:
You can also decided to include the portfolio in your blog as well if you have one.
Note: This example makes use of timthumb. Make sure that’s installed and working, otherwise you’re images won’t resize right.
Step 1 : The Category
Create a category called “Portfolio” in your admin section. This is how we’ll pull your pieces into both pages.
Now, in WordPress you can have multiple templates for everything, you’re not just stuck with the same “category” template for everyone.
Open up a new PHP document and name it “category-portfolio.php” and save it to your theme directory. This will allow you to strip out the side bar and customize the portfolio content without affecting any other category. WordPress will automatically pull the page for you.
Step 2: The Portfolio Code
Now to make the page pull only the first image from the post, and not the title or description, copy and paste this code into your category-portfolio.php page:
<?php get_header(); ?>
<?php query_posts(“showposts=$ebusiness_catnum_posts&paged=$paged&cat=$cat”); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
<?php $thumb = get_thumbnail(); ?>
<div class=”thumb-gallery<?php if ($i%4 == 0) echo(” last”); ?>”>
<a href=”<?php the_permalink() ?>” title=”<?php printf(__ (‘Permanent Link to %s’, ‘Lumin’), get_the_title()) ?>”><img src=”<?php bloginfo(‘stylesheet_directory’); ?>/timthumb.php?src=<?php echo $thumb; ?>&h=123&w=123&zc=1″ alt=”<?php echo the_title(); ?>” width=”123″ height=”123″ class=”preview-thumb” /></a>
</div> <!– end .thumb-gallery –>
<?php endwhile; ?>
<?php get_footer(); ?>
</body>
</html>
Step 3: The Functions
Copy and paste this code into your functions.php file. This defines “get_thumbnail” and also tells it to pull the first image from the post:
function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+src=[\'"]([^\'"]+)[\'"].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
function get_thumbnail()
{
global $post, $shortname, $posts;
$thumb = get_post_meta($post->ID, ‘Thumbnail’, $single = true);
if (($thumb == ”) && ((get_option($shortname.’_grab_image’)) == ‘on’)) $thumb = catch_that_image();
return $thumb;
}
Check your portfolio category site. You can either link to it in your navigation or visit it via your blog. It should now display thumbnails of all your posts from the category portfolio. Of course you’ll need to edit the HTML and CSS!
Step 4: Displaying it on your homepage
Let’s say we want the 5 newest portfolio pieces to show on the homepage. In order to do this we need to call the loop and tell it which category to pull from. Copy and paste this code into your homepage, or wherever you want the latest portfolio pieces to show.
<?php query_posts(‘category_name=portfolio&showposts=5′); ?>
<?php while (have_posts()) : the_post(); ?>
<?php $thumb = get_thumbnail(); ?>
<a href=”<?php the_permalink() ?>” title=”<?php printf(__ (‘Permanent Link to %s’), get_the_title()) ?>”><img src=”<?php bloginfo(‘stylesheet_directory’); ?>/timthumb.php?src=<?php echo $thumb; ?>&h=123&w=123&zc=1″ alt=”<?php echo the_title(); ?>” width=”123″ height=”123″ class=”preview-thumb” /></a>
<?php endwhile;?>
This should now show your newest portfolio pieces!
What did you think of this tutorial?

I like it, Amber! I’ve been thinking about doing something similar to this in my own portfolio; I really like your approach and clear explanation.
Great write-up, thanks for taking the time to do it.
Small note from my side:
When doing a custom query, after endwhile(); it’s always best to reset the query with wp_reset_query(); so if you have another query going on nothing gets messed up.
Cheers!
nice tutorial thanks i do need to learn more word press items
nice post. but code is difficult to read.
please use some plugin to increase code readability.
I was looking for just that and i have gotten a few bits done on these lines already looking at smashingmagazine tuts on the site for wp. But this one is a bonus. I am going to get this done on one of my client sites (a web designer and a good one at it). Thanks a lot your ideas rock and i hope to see more of these here. May be you could add some more tips, specially on creating custom templates and tips on creating plugins. WP is a PIA to learn as it has a whole range of functions completely out of the way from PHP and your site and sites like yours really help ….
Hi,
I am having issues with the code provided here: category-portfolio.php .
Could you update it?
What issues are you having? I’ve checked it on the demo and it works.
I’ll get an: Parse error: syntax error, unexpected $end
Could it be that it is because I use php 5?
No that shouldn’t effect anything….have you tried recopying/pasting the code above and see if perhaps you accidentally deleted something? Also, let me know which snippet you’re copying&pasting from..could be a mistake when I was doing that part
You’re code always looks so clean.
Just a question: wouldn’t the portfolio be easier to manage using custom posts? One of the reasons might be that, if you also have a blog, you don’t have to exclude the portfolio category in all your queries.
Very nice design btw
Yup, this was before custom post types came out
And thanks!