Menu

Katharina Brunner

Bloggin' since 2007

WordPress: How to Add Thumbnails to Google Analytics Popular Post Plugin

Google Analytics Popular Plugin is a great and widely used way to present the most read articles on a website. In its default version it shows the results as an unordered list in the widgets area. Unfortunately adding thumbnails is not included. But there is a workaround.

It takes only a few steps:

1) Open google-analytics-popular-posts.php

2) Look for the code in line 378 – 384 and replace it with the following lines of code:

$titleStr = get_the_title($getPostID);
$post = get_post($getPostID);
$thumb = get_the_post_thumbnail($getPostID);
$dateStr = mysql2date('Y-m-d', $post->post_date);
$contentStr = strip_tags(mb_substr($post->post_content, 0, 60));
$output .= '<ul>'."n";
$output .= '<li>'."n";
$output .= '<div class="popular_post"><a href='.$postPagepath.'>'.$thumb.$titleStr.'</a><br/>'."n";

Explanation: You add a new variable called $thumb by using the get_the_post_thumbnail WordPress function for the specific post via $getPostID. In the $output-variable you call the $thumb variable.

Now you are using the default thumbnail size. If you want to change the size, please look at step 3.

3) To change the thumbnail size, you need to add a new variable that is called $newthumb in this example. $newthumb is equal to the thumbnail size you added via functions.php. Then you have to call $newthumbsize in $thumb.

$titleStr = get_the_title($getPostID);
$post = get_post($getPostID);
$newthumbsize = 'thumbnail-big';
$thumb = get_the_post_thumbnail($getPostID, $newthumbsize);
$dateStr = mysql2date('Y-m-d', $post->post_date);
$contentStr = strip_tags(mb_substr($post->post_content, 0, 60));
$output .= '<ul>'."n";
$output .= '<li>'."n";
$output .= '<div><a href='.$postPagepath.'>'.$thumb.$titleStr.'</a><br />'."n";

Voilá!

(This code is a modified version of a suggestion in the support area of Google Analytics Popular Posts plugin. The piece of code didn’t work for me).

Schreibe einen Kommentar