« | »

Custom WordPress Plugins

WordPressI was looking through my site stats over the past few weeks (nothing to brag about I assure you) and noticed a disturbing trend. Most of the hits on my blog are the result of searches for various topics. I realized most of those topics are related to blog posts which no longer display on my site homepage. I imagine most people are really confused as to why my site came up in their search results if the page doesn’t mention what they are looking for. I suppose I could have put a search box on my site, but I’m not particularly fond of them and besides, my site only has like 25 posts max so there really isn’t a lot of content. I currently only display the seven most recent posts on my homepage. I wanted a way to provide links to the posts which have fallen off the homepage. I couldn’t find a plugin that would do this for me, so I decided to write my own.

You can see it in action for yourself over to the right. It’s the widget called “Older Posts”. I simply created the following PHP file and put it under my /wp-content/plugins folder.

<?php
/*
Plugin Name: Links to My Older Posts
Plugin URI: http://poliorketika.com
Description: Show a list of links to posts not shown on the homepage
Version: 1.0
Author: Poliorketika
Author URI: http://poliorketika.com
License: GPL2
*/
function myOldLinks() {
	echo '<ul style="font-size:11px">';
        query_posts('posts_per_page=15&offset=7');
	if ( have_posts() ) :
		while ( have_posts() ) : the_post();
        echo '<li>';
		echo '<a href="', the_permalink(), '" rel="bookmark" title="', the_title_attribute(), '">', the_title(), '</a>';
		echo '</li>';
		endwhile;
	endif;
	echo '</ul>';
}
function widget_myOldLinks($args) {
	extract($args);
	echo $before_widget;
	echo $before_title, 'Older Posts', $after_title;
	myOldLinks();
	echo $after_widget;
}
function myOldLinks_init() {
	register_sidebar_widget(__('My Older Posts'), 'widget_myOldLinks');
}
add_action("plugins_loaded", "myOldLinks_init");
?>

It’s amazingly simple. Line 13 queries my blog posts to find the 15 most recent posts (posts_per_page) skipping the first seven (offset) because they are already on the homepage. You can change those number to whatever works best for you. It’s also defined to work as a widget which means I can drag it to whichever position I want in the sidebar and it is styled the same as all the others – other than the font-size which I enforced at line 12 because some of my blog titles are rather long and I didn’t want them to wrap. I’m not really in the mood to go into the rest of it because no one is going to read this anyway. If you have read down this far, feel free to use this code yourself in whole or in part if you’re looking for something similar. If you feel you need more explanation, try this article by Andrew Valums.

This entry was posted on Wednesday, August 25th, 2010 at 1:53 pm and is filed under PHP Scripts, WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.



Leave a Reply