• Upon request, I’d like to share my little hack of Jonathan’s great Top10 Posts/Views plugin:
    On my weblog, I use a background-image under every entrie’s headline. But I wanted WP to automatically use a different image when a certain entry has been read very often and thus is likely to be of interest to new readers.

    Here’s how I did it:

    1) Download and install the Top10-Plugin to your plugin-directory.

    2) Follow the instructions that come with the plugin to set up your mysql-database and add the code that counts how often a post has been viewed to your single.php template.

    3) Now, open your index.php template and find the following line:
    <div class="post" id="post-<?php the_ID(); ?>">

    Replace it with:
    <div class="post" id="post-<?php the_ID(); ?>" <?php
    global $wpdb, $tableposts;
    $postcountID = $post->ID;
    $resultscount = $wpdb->get_results("select postnumber, cntaccess from mostAccessed WHERE postnumber = $postcountID");
    if (isset($resultscount)) {
    foreach ($resultscount as $resultcount) {
    $postcount = $resultcount->cntaccess;
    if ($postcount > 35) { ?>
    style="background-image:url('https://www.yoururl.com/img/xyz.gif');"
    <?php }
    }
    } ?>>

    You can customize the amount of views needed (35 in the example above) and of course the URL to the image.

    4) If you don’t want to use an image to highlight popular posts but rather change the color etc. of the headline, here’s how to do it. First, find the following line:

    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    Replace it with:
    <h3 <?php
    global $wpdb, $tableposts;
    $postcountID = $post->ID;
    $resultscount = $wpdb->get_results("select postnumber, cntaccess from mostAccessed WHERE postnumber = $postcountID");
    if (isset($resultscount)) {
    foreach ($resultscount as $resultcount) {
    $postcount = $resultcount->cntaccess;
    if ($postcount > 35) { ?>
    style="color:#FF0000; text-decoration:underline;"
    <?php }
    }
    } ?>><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h3>

    Again, customize the style-tag as needed.

    You might want to apply the above procedure to your archive.php file as well.

    That’s it. See a working example on my website (popular posts have a red (instead of grey) background-image underneath the headline).

  • The topic ‘Highlight popular posts’ is closed to new replies.