Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Show Your Favorite Tweets with WordPress

Just replace the URL in the fetch_rss line below with the RSS feed to your Twitter favorites. In fact this will work with any RSS feed.

<?php
    include_once(ABSPATH . WPINC . '/feed.php');
    $rss = fetch_feed('http://twitter.com/favorites/793830.rss');
    $maxitems = $rss->get_item_quantity(3); 
    $rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
    // Loop through each feed item and display each item as a hyperlink.
    foreach ( $rss_items as $item ) : ?>
    <li>
        <a href='<?php echo $item->get_permalink(); ?>'>
            <?php echo $item->get_title(); ?>
        </a>
    </li>
    <?php endforeach; ?>
</ul>

Comments

  1. Srigi
    Permalink to comment#

    Very ussefull, thanks for sharing.

  2. Nithin Bekal
    Permalink to comment#

    I used something similar for a theme recently, but it involved a lot more code. This will be useful.

  3. Shari
    Permalink to comment#

    How about comment for each favorite? What do you think?

  4. Joe G.
    Permalink to comment#

    what about making a tweet a post? is this something that can be achieved with this code?

    • Chris Radford

      Nope, this code will pull the tweet into the WordPress theme, not the database, you’d need to find a plugin or do that manually.

      Liking the shortness of the code, used something similar recently, was just a lot longer…

    • Matt Royce
      Permalink to comment#

      I know this is well over a year old, but couldn’t you just setup a super long RSS feed with AJAX auto-reload when you scroll to the bottom of the page, and list all of your tweets on there?

  5. Cipha Sublime
    Permalink to comment#

    I added a custom field option to this snippet. Will allow users to specify custom rss from facebook, twitter or where ever.

    <?php // Get RSS Feed(s)
    	include_once(ABSPATH . WPINC . '/rss.php');
    	$feeds =  get_post_meta($post->ID, 'feeds',true);
    	$rss = fetch_rss($feeds);
    	$maxitems = 5;
    	$items = array_slice($rss->items,0,$maxitems);
    ?>
    
    <ul>
    	<?php 
    	  if (empty($items)) 
    	    echo '<li>No items</li>';
    	  else
    	    foreach ( $items as $item ) : ?>
        	  <li>
        	    <a href='<?php echo $item['link']; ?>' title='<?php echo $item['title']; ?>'><?php echo $item['title']; ?> </a>
        	  </li>
    	    <?php endforeach; ?>
    </ul>
  6. Robert Merrill
    Permalink to comment#

    Nice snippet. What about accessing private feeds with this… any way to authenticate the feed?

  7. seo
    Permalink to comment#

    Thank you, I have recently been seeking for details about this subject matter for ages and yours is the best I’ve discovered so far.

  8. Nick Hauselman
    Permalink to comment#

    I’ve been having trouble implementing this to a post. Can you suggest a plugin that will allow PHP on a post that works well with this?
    I’ve been struggling with Allow PHP in Posts and Pages and I’m getting all kinds of errors. THANKS.

  9. Bharat Chowdary

    Thanks for saving my time, works fine on my blog.

  10. Jon
    Permalink to comment#

    Doesn’t seem to work on Windows server.

  11. Vishnu
    Permalink to comment#

    I tried this…i get error as “Too many redirects”
    I cant login to my blog untill i deleted the plugin manually.

    Am i doing something wrong?

  12. Fidelis Agi
    Permalink to comment#

    kool…. definitely going to use this ish on my next project.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

```
<script>
  function example() {
    element.innerHTML = "<div>code</div>";
  }
</script>
```