WordPress 3: Creating and Editing Custom Themes
Illustration by John Hersey

Fetching external content


WordPress 3: Creating and Editing Custom Themes

with Chris Coyier

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Fetching external content

The last thing we want to do in our sidebar is display some content from an external site. Basically, we're going to be pulling in from an RSS feed some content to fill out the sidebar. So again, this content that exists here now looks how we want it to look, but it's just static content. So I have opened our WordPress theme as a project in TextMate and we're working with the sidebar.php file. This bit of content here is what we're looking at over here.
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 6s
    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

please wait ...
Watch the Online Video Course 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
Developer Web
Chris Coyier

Fetching external content

The last thing we want to do in our sidebar is display some content from an external site. Basically, we're going to be pulling in from an RSS feed some content to fill out the sidebar. So again, this content that exists here now looks how we want it to look, but it's just static content. So I have opened our WordPress theme as a project in TextMate and we're working with the sidebar.php file. This bit of content here is what we're looking at over here.

It's just that static content as this h4 tag of Industry News, and then just these static dates, static titles, and Lorem Ipsum text for the excerpts. We're going to be replacing that by dynamically pulling in content for that. So we're going to be using a special built-in WordPress function called fetch_feed, which is going to be doing all that magic for us. So we're going to start it right here. Remember how when we were in the functions.php file and we were declaring those menus and we were past proofing it by making sure that function exists? We're going to do that same thing here by just making sure that fetch_feed function exists before we use it.

So if (function_exists ('fetch_feed')), then do stuff. So everything that we're going to do is kind of be within these. We only need one of these because we're going to be looping, so we will just save one of them as kind of a template. Put that within here. Now, we've proven because we're inside this if statement that this function exists, but in order to use it, we're going to need to do an Include for the file.

That kind of enables all the stuff to work. So we'll just use the standard include_once function, and this stuff here, ABSPATH and WPINC, are constants that are declared that WordPress just knows about, and it's going to make sure that we get to the right path despite what's going on in our theme and despite other settings and stuff like that. So this is just the smart way to do it. /feed.php. We need this feed file for this to work at all.

You know what, this is actually PHP. Now we're going to call this fetch_feed function. So we're going to actually make a variable for it called feed. We're going to set it to fetch_feed. The parameter that you pass fetch_feed is an RSS feed. So wherever this RSS feed lives, I happen to have one handy here, so that's just add http://www.topix.com/ rss/business/manufacturing.

In my limited searching, that was the fastest one I could find that had anything to do with a widget. So we'll play with that in a minute but that's basically how you do a fetch_feed. Well we have to set some things like the limit is going to be our feed object, get_item_quantity. We're just going to say we only want to get two things because in our mockup over here we have two loops. So just don't waste time. Just only get two. And we're going to set up a new variable. items, and it's going to be our feed object and we're going to say get_items from 0 to the limit.

Just setting ourselves up here, so that we'd be able to loop through these items and output them properly. So first we want to make sure that it works. So if the items variable doesn't really have anything in it, then we have a problem. We're going to have to echo something out, some kind of a problem. So, we'll just say problem for now. But if it does exist, which it probably should, we'll say else. So everything is cool in here.

If it's cool, we're going to need to loop through these items. So we're going to do a foreach loop, just a real standard PHP loop. So foreach ($items as $item). It's going to mean that item is one individual kind of RSS entry and in this foreach loop is then finally this content. Get rid of this. Put that in that foreach loop. We're going to have to make sure this is happy. PHP tags are being closed properly and opened properly and such.

That should get us right. So this is the loop. So this loop is going to run twice. We've said we want two items, so there're going to be two objects in this items and each individual one is item and now we have what we need to replace the static content with dynamic content. So we're going to try echo and we have access now to this individual item and that's an object. So we'll use the get_date function on it and we can format the date however we want, like the month, day, year is pretty standard.

For the title, we'll echo up the item again. Now, this is PHP. It keeps slipping in and out of PHP here, so php tags, echo, the item, the get_title, maybe we already used that one, and the content. One last thing. Get rid of this Lorem Ipsum and we'll see. get_content.

So there we go, closing the foreach, and let's see. What is this one? This one closes the If. I think we might have an extra one in here. Hopefully that doesn't cause an error. We will know soon enough. We're going to need to end that foreach loop though. That's what this does, right? Let's save it and see how we're doing over here. We've caused an error somewhere. I bet we can track it down now.

In fact, we didn't have an extra one here. That was natural. That was closing the If statement up here. So the foreach is closed, the If is closed, and the if function_exists is closed. So all of our things are being closed here nicely. If we hit save and reload our theme, indeed we're getting content from that external RSS feed over here. One last thing is we're hoping to link those titles out to those places. So let's make an anchor tag out of our titles, close that anchor tag, and then as the href for this link, we'll echo out one last thing, the permalink.

So now those are linked to the articles as they should be. So what's kind of neat there is that we can just plop in basically any RSS feed and display content from those external RSS feeds, and WordPress is real smart and that it caches. So it's not like every single time this page loads, it has to go out and hit this external resource. It's just intelligent about caching that information. In fact, lynda.com has an RSS feed and we could replace this RSS feed with lynda.com's blog RSS feed, and just hit Save and reload the page over here, and instead of displaying those manufacturing related articles, it's going to show actual lynda.com blog articles.

It's going to have too much content in here probably. We can write some PHP to truncate that content if we want to, but let's just leave our manufacturing feed in there, which displays nice excerpts already. So that's how easy it is to use an external RSS feed, grab content from it, and display it right on your own site, using this fetch_feed function which is a built-in WordPress function.

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


    <?php endwhile; ?>

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

    <?php else : ?>

        <h2>Not Found</h2>

    <?php endif; ?>

<?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

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.

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.

* Estimated file size

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


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

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

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 ?

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 preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.