官术网_书友最值得收藏!

Displaying your Twitter entries on your blog using a page template

It is possible to create a Twitter page on your blog with the help of the Twitter Tools plugin. How about playing a bit with the code in order to achieve the same result without using any plugin?

Getting ready

To achieve this recipe, you'll need to use a page template. Page templates will be discussed later in this chapter. Therefore, I won't get into details; just the basics—a page template is a PHP file with a custom layout that you can use on your WordPress blog.

How to do it...

  1. Let's start by creating a very basic page template. Create a new file on your computer and name it, for example, twitter.php and insert the following code in it:
    <?php
    /*
    Template Name: Twitter Page
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    <?php //Content goes here ?>
    
    </div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. The most important thing to remember while attempting to create a WordPress page template is the template name—your page name comment is always located at the beginning of the file. This PHP comment allows WordPress to know that this file is a page template, and then you'll be able to use it.
  3. As you can see, there's nothing hard with page template coding. We just need to import blog header, footer, and sidebar.
  4. Now that we have our basic page template layout, we can start to integrate the Twitter entries. Each Twitter account has RSS feeds.
  5. The first thing to do is to get the feed URL. To do so, just visit your own Twitter page and get the feed. It might look similar to the following link: http://twitter.com/statuses/user_timeline/15985955.rss
  6. To integrate this feed into our page template, we shall use the rss.php file from WordPress core. This file will allow us to use the wp_rss() function, which is a built-in RSS reader. The function can read any RSS feed and display it on your WordPress blog.
  7. Let's go back to our page template code, and add the following code instead of the <?php //Content goes here ?> comment:
    <?php include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://twitter.com/statuses/user_timeline/15985955.rss', 10); ?>
  8. In the preceding code, we first include the requested rss.php file and then we call the wp_rss() function, which will read our Twitter feed and display it on our page template.
  9. The wp_rss() function takes two arguments—the first argument is the RSS feed URL and the second is the number of entries you'd like to display. In the following example, I set 10 as the parameter but it's up to you to choose the number of entries you wish to display. Our final Twitter Page code now looks like this:
    <?php
    /*
    Template Name: Twitter Page
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    
    <?php include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://twitter.com/statuses/user_timeline/15985955.rss', 10); ?>
    
    </div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  10. Save the file under the name twitter.php.
  11. Now that we have our Twitter Page ready, I'm pretty sure that you can't wait to use it. Great! Upload the file to your wp-content/themes/yourtheme directory and login to your WordPress Dashboard.
  12. Create a new page and name it Twitter entries, for example. You don't have to write any content because it isn't needed and our page template doesn't have a function such as the_content(). Therefore, it simply cant display any content. The purpose of this page is to display your twitter entries.
  13. To use the page template, scroll down until you see the Page Template drop-down box. In the drop-down menu options, select Twitter Page (the name of the template) and publish the page.
  14. You can now go visit your blog. The Twitter entries will be displayed on the page we just created.
    How to do it...

How it works...

To achieve this recipe, we had to make use of a Page Template. Page Template will be discussed later on this chapter so you'll hopefully find an answer to all questions you are currently asking yourself.

The second part of the code is used for parsing the RSS feeds using the PHP language. To learn more about the RSS feeds parsing, you can visit the following link and read the entry on my blog: http://www.wprecipes.com/how-to-display-your-latest-twitter-entry-on-your-wp-blog.

主站蜘蛛池模板: 太谷县| 双牌县| 福州市| 东平县| 嘉兴市| 出国| 报价| 上饶县| 浦城县| 昭觉县| 长宁县| 徐闻县| 马龙县| 平乐县| 收藏| 东源县| 东乌珠穆沁旗| 台中县| 南川市| 新邵县| 邵武市| 衢州市| 长兴县| 乌鲁木齐市| 轮台县| 清原| 日喀则市| 宁蒗| 尼勒克县| 阿荣旗| 宜章县| 龙陵县| 任丘市| 鄯善县| 仪征市| 吉安县| 新余市| 新蔡县| 惠州市| 宣汉县| 聂荣县|