Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Fetching external content

From: WordPress 3: Creating and Editing Custom Themes

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.

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.

Show transcript

This video is part of

Image for WordPress 3: Creating and Editing Custom Themes
WordPress 3: Creating and Editing Custom Themes

40 video lessons · 51843 viewers

Chris Coyier
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.

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

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.