Thursday, July 22, 2010

Thumbnails for WordPress.com Popular Posts

While working on the Latest Gadgets review blog I installed a plugin called WordPress.com Popular Posts which uses stats collected from your blog to work out the most frequently viewed stories during a time frame of your choice. The installation is very easy and customizing the plugin can be done via the widgets page. However I felt it was missing something: thumbnails!

On the site I was working on we use the first image in a post as the source image for our thumbnails and use the timthumb.php function to dynamically resize them. I found this blog posting which described how to create thumbnails for an alternative "popular posts" plugin. With a bit of hacking at the code provided there I was able to get thumbnails for the WordPress.com Popular Posts plugin.

So, with thanks to the aforementioned blog, here is a record of what I did for future reference (and for the benefit of anyone searching for a similar solution).

You need to download and install timthumb.php if your theme doesn't currently use it. Then I used the code provided here and added it to my theme's functions.php file (sorry, Blogger isn't good at showing code, so no indentation!):
/**
* Capture the first image from the post.
* @global object $post
* @global object $posts
* @return string
*/
function theme_function_capture_first_image($p=null) {
$firstImg = '';
if (empty($p)) {
global $post, $posts;
$firstImg = '';
ob_start(); ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$firstImg = $matches[1][0];
} else {
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $p->post_content, $matches);
$firstImg = $matches[1][0];
}
return $firstImg;
}
Then I edited the wordpresscom-popular-posts/wppp.php file. First find this code:
// Replace format with data
$replace = array(
'%post_permalink%' => get_permalink( $post['post_id'] ),
'%post_title%' => esc_html( $this->truncateText( $post['post_title'], $instance['title_length'] ) ),
'%post_title_attribute%' => esc_attr( $post['post_title'] ),
'%post_views%' => number_format_i18n( $post['views'] )
);
Then add this code after, editing the paths as necessary:
// thumbnail
$img = theme_function_capture_first_image(get_post($post['post_id']));
$src = get_permalink($post['post_id']);
$imgpath = '';
if (empty($img)) {
// define path to default thumbnail
$imgpath = 'http://___PATH_TO___/default.gif';
} else {
// define path to timthumb.php
$imgpath = 'http://___PATH_TO___/timthumb.php?' . 'src=' . $img . '&amp;w=80&amp;h=40&amp;zc=1';
}
$replace['%post_thumb%'] = '<img src="' . $imgpath . '" alt="'. $title .'" />';
Then you can use %post_thumb% in your formatting options (controlled in the widget). If you want to change the image size then edit the w=80 and h=40 in code above.

You might also want to add some CSS lines to your themes CSS file to make it look pretty, I used the following:
ul.wppp_list li {
min-height:45px;
display:block;
list-style:none outside none;
}

ul.wppp_list img {
float:left;
margin-right:8px;
width:80px;
height:40px;
border:2px solid #ECEFF5;
}

I hope that helps :)

Thursday, April 22, 2010

Viewing US ads & results in Google

Today I was doing some research and wanted to know how to view Google ads (via Adwords) that were focused at the US market. Typically, if you do a search for a keyword in the UK, Google sees you are from the UK and shows you ads from UK companies. I found the method thanks to this post. If you want to see ads that would be shown to people in the US, you just need to add &gl=US to the URL after you've done your search, for example http://www.google.com/search?hl=en&gl=US&q=geoffblog

Thursday, January 28, 2010

"PC Not Listed" XBox 360 - Solution

This was driving me mad, my PC (XP SP3) could see the XBox from Windows Media Player 11, but for some reason the XBox kept reporting "PC Not Found". I searched the internet for hours and tried every solution under the sun, including the renaming/deleting of the DRM folder, disabling firewalls (both Kaspersky and Windows) as well as fiddling around with settings in the Netgear router. Nothing worked. Then I thought I'd try running the Windows "Set up a home or small office network" wizard found inside Network Connections, just to ensure that the settings within Windows were correct for networking. Hey presto, the XBox could now, finally, see the PC. I hope this helps anyone else who is pulling their hair out on this!