Start learning with our library of video tutorials taught by experts. Get started

WordPress 3: Creating and Editing Custom Themes

Creating custom product pages


From:

WordPress 3: Creating and Editing Custom Themes

with Chris Coyier

Video: Creating custom product pages

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.
Expand all | Collapse all
  1. 6m 44s
    1. Welcome
      1m 19s
    2. Using the exercise files
      5m 25s
  2. 40m 42s
    1. Reviewing the client spec and deciding on WordPress
      6m 50s
    2. Reviewing assets and resources and creating a mood board
      8m 41s
    3. Building a home page mockup
      11m 26s
    4. Finishing the home page
      12m 27s
    5. Planning the rest of the site
      1m 18s
  3. 1h 8m
    1. Starting with a base project
      3m 6s
    2. Writing HTML code for the home page
      12m 7s
    3. Starting the CSS: Creating the header and basic style structure
      11m 28s
    4. Styling the Navigation panel
      10m 59s
    5. Styling the sidebar
      7m 55s
    6. Styling the home page, pt. 1
      8m 20s
    7. Styling the home page, pt. 2
      8m 17s
    8. Finishing the CSS
      3m 14s
    9. Moving on: One page is enough
      2m 43s
  4. 1h 56m
    1. Setting up WordPress and MAMP on a Mac
      6m 7s
    2. Setting up WordPress and WAMP on a Windows computer
      5m 38s
    3. Modifying important settings
      6m 26s
    4. Starting with a blank theme template
      4m 35s
    5. Introducing template file structure
      4m 55s
    6. Breaking up the HTML
      9m 53s
    7. Building the sidebar
      3m 54s
    8. Building the navigation
      7m 20s
    9. Showing one recent post
      4m 1s
    10. Fetching external content
      8m 23s
    11. Creating a custom home page
      3m 30s
    12. Introducing custom fields
      5m 23s
    13. Creating custom product pages
      9m 52s
    14. Creating custom category pages
      15m 39s
    15. Creating the blog home page
      5m 39s
    16. Creating a single blog entry page
      4m 15s
    17. Implementing comments
      5m 57s
    18. Finishing the home page
      4m 45s
  5. 34m 17s
    1. Will this work with WordPress?
      3m 10s
    2. Using JavaScript in themes the right way
      8m 35s
    3. Implementing something fun with JavaScript
      7m 53s
    4. Introducing plug-ins
      6m 31s
    5. Setting up security
      8m 8s
  6. 2m 7s
    1. Goodbye
      2m 7s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
WordPress 3: Creating and Editing Custom Themes
4h 28m Intermediate Nov 03, 2010

Viewers: in countries Watching now:

In WordPress 3: Creating and Editing Custom Themes, author Chris Coyier shows how to build a custom WordPress theme from scratch and satisfy common client requests. The course covers steps necessary to build a theme using a complete workflow with Photoshop, HTML, CSS, and WordPress 3.0. Also included are tutorials on enhancing a WordPress site with JavaScript, using plugins, and ensuring site security. Exercise files accompany the course.

Topics include:
  • Building a design in Photoshop
  • Converting Photoshop design to HTML and CSS
  • Setting up MAMP on Mac and WAMP on Windows
  • Moving HTML and CSS into a WordPress theme
  • Building navigation
  • Using custom fields
  • Creating a commenting system
  • Using JavaScript and plugins
Subjects:
Developer Web CMS
Software:
WordPress
Author:
Chris Coyier

Creating custom product pages

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.


Expand all | Collapse all
Please wait...
Q: What prerequisite skill do I need to be successful in this course?
A: This course is set at the intermediate/advanced level. You’ll do best if you have a good knowledge of Photoshop, plus a good grasp of PHPHTML, and CSS.

Q: The index.php file that the author is working with in Chapter 3 doesn't match mine after the "Building a sidebar" movie. It appears to change between the "Building a sidebar" and "Building the navigation" movies. What code am I missing?
A: The author makes some changes off screen between several movies in this title, simply because there is so much material to cover. These changes are provided in the exercise files.

However, if you are following along without the exercise files, you catch up to him by adding the following code to your index.php file, directly after the <?php get_header(); ?> line:

<div id="main-content">

Near the end of the file, just before  <?php get_sidebar(); ?>, add a closing div tag, </div>, to complete the div wrapper.

The resulting code will look like so. You may also copy and paste this into a new file and save it as index.php.

<?php get_header(); ?>

<div id='main-content'>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

            <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

            <div class="entry">
                <?php the_content(); ?>
            </div>

            <div class="postmetadata">
                <?php the_tags('Tags: ', ', ', '<br />'); ?>
                Posted in <?php the_category(', ') ?> |
                <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
            </div>

        </div>

    <?php endwhile; ?>

    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

    <?php else : ?>

        <h2>Not Found</h2>

    <?php endif; ?>
   
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Q: How do I load my custom theme once I have finished?
A: Copy the Custom theme folder to your new WordPress installation and put it in wp-content > themes. Then you can activate the new theme and work with it from there.
Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress 3: Creating and Editing Custom Themes.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked