Tag Archives: post

WordPress Hack – Displaying Popular Posts

12 Feb

On my quest to improve this blog, I just added a way of keeping track of how many views each post has, as well as a list of the most viewed (see the footer).

I followed a tip from Pedro (blog in Portuguese) and went for the WP-PostViews plugin. It basically adds a couple of fields in the database and some functions to update each post’s view count and display it.

The installation is very easy. It’s all explained here, including how to use it. Basically: install (you can use the WordPress auto installer), activate, and then add a snippet of code wherever you want the info displayed.

<?php if (function_exists('get_most_viewed')): ?>
   <ul>
      <?php get_most_viewed(); ?>
   </ul>
<?php endif; ?>

That’s the code to display the most viewed posts. You can also display the least viewed and the count for each individual post.

More plugins can be found from the same guy who did this one, Lester Chen. I might take a look at them in the future, there seems to be some neat stuff there.

WordPress Hack – Float a Specific Set of Posts to the Top of the Main Page

9 Feb

This is the first post of what I expect to become a series about WordPress hacks, i.e., small tweaks that I’ll be doing to customize this blog’s WordPress functionality.

The most observant of readers might have noticed something that can look a bit weird. The order of the posts in the blog’s main page is not the same as the order in “Recent Posts” or in the RSS Feed. What’s happening is I’m making some posts “sticky”, that is, they’ll cling to the top of the homepage even if I post something else afterwards, instead of just having all the post showing up in reverse chronological order.

Let me explain the reason for this. The main objective of this blog is to share my personal adventures now that I left my hometown to work abroad. But sometimes I like to post some other random stuff, like the review of a TV show, some cool web application,  a rant about something that’s annoying me, or just something funny I found on the Internet. To help keeping the blog on track, I’ve chosen to always float the latest post from the Personal category (the one where I file all the posts related to my day-to-day happenings) to the top. This way, blog visitors will always see one post from that category at the top of the homepage.

OK, now for the hack itself. The WordPress main page is basically a loop that goes through all the published posts and displays them:

<?php
if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
  // display post and related metadata
<?php endwhile; ?>

So, to achieve the intended functionality, I added some code to display the post I want floated to the top. It’s basically the same as the main loop, but I use WP_Query() to retrive the post I want to display. Note that you need to store the id of the post in some variable so that you can skip it in the main loop (or else it would show up twice). Also note that some tweeking has to be done to account for a WordPress “feature” that makes the tags on the first post to disappear. The resulting code (cropped for simplicity) is:

// sticky post(s)
<?php
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('category_name=Personal&showposts=1');
while ($wp_query->have_posts()) : $wp_query->the_post();
  $do_not_duplicate = $post->ID;?>
  // display post and related metadata
<?php endwhile; ?>

// all the rest
<?php
//query_posts('');
$wp_query = null;
$wp_query = $temp;
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate) continue; ?>
  // display post and related metadata
<?php endwhile; ?>

You can easily control what you want displayed on top by parametrizing WP_Query() as you like. And the only change will be in the homepage, everything else will be in it’s “normal” state.

UPDATE: It seems that this “hack” caused the pagination to stop working. To solve I just had to assign the $temp value we stored in the beginning to the $wp_query variable. The code above has been corrected. Full explanation here.