- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 639字
- 2021-04-01 13:55:50
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...
- 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(); ?>
- 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.
- As you can see, there's nothing hard with page template coding. We just need to import blog header, footer, and sidebar.
- Now that we have our basic page template layout, we can start to integrate the Twitter entries. Each Twitter account has RSS feeds.
- 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
- 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 thewp_rss()
function, which is a built-in RSS reader. The function can read any RSS feed and display it on your WordPress blog. - 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); ?>
- In the preceding code, we first include the requested
rss.php
file and then we call thewp_rss()
function, which will read our Twitter feed and display it on our page template. - 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(); ?>
- Save the file under the name
twitter.php
. - 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. - 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. - 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.
- You can now go visit your blog. The Twitter entries will be displayed on the page we just created.
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.
- Creo Parametric 8.0中文版基礎入門一本通
- Adobe Photoshop 網頁設計與制作標準實訓教程(CS5修訂版)
- Photoshop CS6 互聯網應用設計教程
- 從零開始:AutoCAD 2015中文版機械制圖基礎培訓教程
- 剪映:短視頻剪輯/字幕/動畫/AI從新手到高手(手機版+電腦版)
- 微信小程序開發入門與實踐
- Drupal Multimedia
- Rhino 6.0中文版入門、精通與實戰
- DWR Java AJAX Applications
- 影視動畫場景與特效制作:3ds Max-Vue-AfterBurn-FumeFX
- Mastering phpMyAdmin 3.1 for Effective MySQL Management
- Photoshop CS6案例教程(第3版)
- PostgreSQL Replication
- 神奇的中文版Photoshop CC 2017入門書
- Flash CC動畫制作與應用(第3版)