Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Widget Corp is a company that sells widgets. So there is going to be a bunch of pages on this site that are dedicated to specific products that they sell. So that's what we're looking at here is our design for an individual product page. We just covered custom fields and how we can store this data, like price and product code as custom fields, but we didn't yet cover how to create a custom template to publish and make the page look exactly like this. What we're looking at on screen here is our static HTML and CSS template.
So let's create a special new template in our WordPress theme, which we have opened on right here, and our static HTML and CSS on the left here. I'm going to create a brand new template by right-clicking the project folder here and saying New File. We're going to call it page-product.php. It's going to be a blank file to begin with. So we're creating a brand new template. How do we get WordPress to recognize these new templates? It's a really simple just PHP comment at the top of the page.
Just like this, and we're going to start a comment like that, end the comment like that. We'll say Template Name. That's the important part. We're going to call it Product Page. So now that we've done that, WordPress is just going to see that this is present in our theme and allow us to pick this template when we're creating new pages. So let's jump back into the Dashboard. We can do that here. We have our Dashboard open for our project. If we go under Pages, you'll see that we've already published two different products here.
I'm going to click it to edit it. This is where we created those custom fields and all that. Now, all of a sudden, this has just popped in, is a drop-down menu where we can pick which template we want this page to use. Now, by default, it's just going to use page.php. But we're going to pick that we want to use the Product Page, the brand new one we just created, and hit Update. If I'm going to Command+Click to open this in a new tab, it's just going to be barren, white, nothingness, because now it's using this new template, and there is nothing in there for it to show.
So there is not going to be anything there. For the Homepage that we created, we use this index as a template. We'll do the same thing there. I'm just going to Select All and Copy everything from index and just paste it over here. If we save that, we'll see that basically this is going to look just like our Homepage then, showing the product in that template. We don't want that. We have our own ideas here. So I'm going to delete everything from main content, between that opening and close div. Hit Save. Now we have this nice blank template to work from.
What we want to fill that with is we'll jump into our HTML and CSS, and we have a special HTML file that's just for store products. So I'm going to copy and paste within the main content div over here, and paste it over here. I'm going to hit Save and jump back to the web, and you can see then that we're getting there as far as a special template just for products, and we have a few things to shore up here. One is we'll fix this file path to make sure that it's pointing at the root product images directory there.
And then we have a bunch of static. It's statically displaying the price. It's statically displaying the dimensions here. We want to make that all dynamic. That's the whole point of this, is this is a template that can be used for any products. So why don't we replace these right away with the custom field values that we had? So instead of displaying just $519 like that, php echo, we're going to echo out using a special function called get_post_meta(). Now, the get_post_meta function has three parameters.
You have to tell it what post, so we can get that from the post object. It's ID. We need to tell it which custom field exactly. We can tell which custom field exactly by looking at the custom fields that we published. One is called price. That's what it's asking for here. Put price in there, and then a true or false. Now, we're going to say true here. True means just give us a string that we can easily echo out. If there are multiple price values, just give me the first one. If we're doing something like selling t-shirts, where we might have size as a custom field, and then have small, medium, large, extra large, stuff like that, you could use false and it will give you an array and you can iterate over that array and display all the available sizes.
We don't need that. We're just going to keep it real simple with putting true as that third parameter. Well, let's Copy and Paste this and use it in the same way for product_code. product_code, copy and paste here, so we don't make any mistakes, and dimensions. So let's hit Save. Now, this is an important point to cover here is that this function is an in loop WordPress function.
It means that it needs to kind of be within the WordPress loop. We don't really need a full loop here, because we're only displaying one product. We'll never display two products on a single product page. It's just not what we're doing. So we don't need to open the loop necessarily. We're just going to say the_post. Remember we used this in the sidebar when we did a query post to get the most recent blog post. We needed to use that so we could use functions like the_title and the_permalink and stuff like that. Now that this is in place, these functions will work.
So let me hit Save and come back out here and reload. We fixed that file path, so it's there. It's displaying the price and code and dimensions now dynamically. Notice we lost the dollar sign there. It's kind of proof that it's working. But we can get that dollar sign back just by altering the template, putting a dollar sign there, and reloading, and that will show up. So in our example where Widget Corporate has come to us months later and say we want to remove all the prices from all the product pages, now, of course we can just make one edit in this template file, save it, and that's going to go away in our thousands of products.
We don't have to edit it one by one. We can just make this one change to a template file and have that go. There are a few other things that we should make dynamic here. One is the title of the product. So we could get rid of that and just echo out the_title. The other is the file path for this image. So I'm just going to cut that away. We're going to need access to it dynamically. So let's make a brand new custom field as a part of this page in WordPress.
It's not in the drop-down menu. It doesn't exist yet. We'll say enter new, and we'll say product_large, because there's a few other versions of this image. The medium_one and the small_one, and we're after the large one for now. So we'll hit Add Custom Field. It will flash yellow, proving that it's saved. Then we'll use one of these custom field echoing statements as the source for that image. So it's going to be product_large instead of price. So if we hit Save and come back out here, now we're going to be fully dynamic.
The title is coming from that. The title looks a little different, doesn't it? So why is that? We'll take a look at our static code. There are a couple of style differences actually. The price had this block over here where all this stuff is saved. That's because there are some custom CSS that we want to apply for this page that is only for this page. So it doesn't matter on the homepage. It doesn't matter on the Contact Us page. It's CSS that's specific to this page. So in order to kind of save bandwidth and keep our CSS smart and modular, we're only going to load up that CSS on this page.
So how do we do that? First, let's take a look at where that CSS is. So in our static HTML and CSS template, we have a css folder. This file here, product.css, deals with that CSS specifically. So let's make sure it's available in our theme. I'm going to Option+drag it over here to the css folder that's in our theme. Then we're going to open up the header. php file as a part of our theme, which has the head section in it, which is where we link up CSS files. Here is the main link for our main CSS file.
We have that again, only instead of the stylesheet_url, which links directly to it, we're going to link to the template_ url, which just links to the folder, and then say /css/product.css. Now, that would load it on every single page of the site. We only want to load it when this specific template is being used, and we can do that with a WordPress conditional tag called is_page_template.
Now, which template? We have to give it to it by name, and we've named ours page-product.php, so page-product.php. Then we'll make sure to close this if statement. We'll hit Save, and so it will load up this product.css file only on product pages, which of course this is. Looks like we made an error. if is_page_template.
Then our title looks right. Our pricing information looks right, just how we had it styled in our original template. So that's how you create a custom page template using custom fields to create a unique page template within WordPress.
Find answers to the most frequently asked questions about WordPress 3: Creating and Editing Custom Themes.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.