- 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.
- UG NX10.0從新手到高手
- VMware虛擬化與云計算:vSphere運(yùn)維卷
- PPT 2016幻燈片設(shè)計與制作從入門到精通
- VSTO 3.0 for Office 2007 Programming
- ASP.NET MVC 2 Cookbook
- SolidWorks2014基礎(chǔ)實例教程
- BIM與Unity 3D開發(fā)實例詳解
- Final Cut Pro短視頻剪輯入門教程
- Photoshop+CorelDRAW平面設(shè)計實例教程(第4版)
- SolidWorks 2018快速入門及應(yīng)用技巧
- Building Websites with VB.NET and DotNetNuke 4
- Microsoft Azure: Enterprise Application Development
- AI繪畫與攝影實戰(zhàn)108招:ChatGPT+Midjourney+文心一格
- Microsoft SQL Server 2008 High Availability
- Premiere短視頻制作(第2版·全彩慕課版)