24 March 2009 @ 10:57Add some Twitter chatter to your WordPress posts

Even if you don't get a lot of comments on your posts, it's still interesting to see what other people are saying about that topic. One way to find what people are saying is to search Twitter for particular keywords. Now, you can add those results to the bottom of your WordPress post and style them (if you want) to match your comments. Here's how:

First, copy/paste the following code into your comments.php. Put it above or below the comment loop.

<!-- start Twitter chatter -->
<?php
if (post_custom('twitter-keyword') != NULL) {
require_once (ABSPATH . WPINC . '/rss-functions.php');
$rss = @fetch_rss('http://search.twitter.com/search.rss?q='.urlencode(post_custom('twitter-keyword')));
if ( isset($rss->items) && 0 != count($rss->items) ) {
$rss->items = array_slice($rss->items, 0, 10);
echo '<h3 id="respond">Twitter chatter:</h3>';
echo '<ol class="commentlist">';
foreach ($rss->items as $item ) {
$hreffix = str_replace('href=', 'target="_blank" rel="nofollow" href=', $item);
$usernamefix = str_replace('@twitter.com', '', $item);
?>
<li class="item">
<div class="mycomment"><img src="<?php echo $item['google']['image_link']; ?>" class="avatar avatar-60 photo" width="60" height="60" />
<div class="com"><span class="author"><?php echo $usernamefix['author']; ?></span> | <small><a target="_blank" rel="nofollow" href="<?php echo $item['link']; ?>"><?php echo human_time_diff( strtotime($item['pubdate'], time() ) ); ?> <?php _e('ago'); ?></a></small></div>
<div class="comment-in">
<?php echo $hreffix['description']; ?><div style="clear:both; height:1px; overflow:hidden;"></div>
</div>
</div>
</li>
<?php
} echo '</ol>';
}
}
?>
<!-- end Twitter chatter -->

Next, on a per-post basis add a Custom Field named twitter-keyword and for the value add any keywords related to your post. You should test the keywords to get a feel for what will be returned.
Custom Fields

And that's all there is to it!

See this post for an example.

Be Sociable, Share!

by | Add a comment | Tags: , , , ,
Posted in wordpress | Link to this

Add a Comment

Show who you are with a Gravatar.

 

Sign up for PayPal and start accepting credit card payments instantly.

Staples Logo

Get fed!

rss icon subscribe to Geek stuff

rss icon Geek stuff in your inbox

Add the "Geek stuff" Google Gadget to your homepage

Add the "Daily Deals" Google Gadget to your homepage

Apparel

Search Amazon

Search Amazon.com
Search Amazon.co.uk

Recent Forum Topics

    Advert

    Web hosting by ICDSoft