This week I wanted to create a page that would hold all of the blog posts that I made in the category ‘Portfolio’. I didn’t create a portfolio page because I wanted to be able to add a post each time I had a new item to add to my portfolio.
<authors note: I have about 100 things to add to this portfolio. If you would like to see my complete portfolio, please visit my website, CimmeronStudios.com>
So, I hit the Wordpress CODEX to dig into the best way to create a page that would display posts based on whatever variable I wanted to add, in this case, the category ID.
To begin, be sure to create a new TEMPLATE for your page. To achieve what we want here, we want to MODIFY the existing LOOP. Now, the LOOP is basically what powers your Wordpress site. Here’s the very basics (taken from Wordpress.org)
The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags.
For a very detailed explanation of the LOOP, check out the Wordpress Codex.
So, back to our page. I’m going to run through this in a step by step fashion.
1. Create a new page template. You can do this by taking the existing page.php and saving it as a template, such as template-portfolio.php. To do that, you will need to remember to place a bit of code at the top of the page so that WP recognizes that it is a template:
2. Locate the following line of code on this new template page:
3. Next, we will alter this part of the code to display only what we WANT to have displayed. We will do that by using a query:
There are a TON of variables that we can use at this point, however, I’m going to use just 2 for this example. Category ID and Number of Posts to display.
The Query has to be added before the loop to make this work, so you will insert your Query like so:
The highlighted area (above) shows you where you’ll be adding your information. Basically, the area between the ( and the ‘ ….
That is where you tell the page WHAT to display. In my example, we have:
5 posts will be displayed on this page
Next we have the important one:
This means that ALL POSTS from Category ID number 19 will be displayed.
You can change this by using the Category name, as well:
After you change out your Query values, you can upload the template to your server, go to the PAGE and assign it to that TEMPLATE – That’s all there is to it!
Here are a few other items that you may want to use when customizing. REMEMBER: You can string multiple variables together by using the & as long as you keep everything within the (‘ ‘):
That should be enough to get you what you’re after. Any questions? Feel free to send me an email or comment below!