Skip to content

Instantly share code, notes, and snippets.

@vinhboy
Created May 11, 2010 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhboy/0c80a1918d6f66683f42 to your computer and use it in GitHub Desktop.
Save vinhboy/0c80a1918d6f66683f42 to your computer and use it in GitHub Desktop.
Show the latest posts with dates
<?php
/*
Plugin Name: Latest Posts
Plugin URI: http://vinhboy.com
Description: Show the latest posts with dates
Author: vinhboy
Version: 1
Author URI: http://vinhboy.com
*/
add_action("widgets_init", array('Widget_latestPosts', 'register'));
register_activation_hook( __FILE__, array('Widget_latestPosts', 'activate'));
register_deactivation_hook( __FILE__, array('Widget_latestPosts', 'deactivate'));
class Widget_latestPosts {
function activate(){
$data = array('size'=>'10','title'=>'Latest Posts');
if (!get_option('widget_name')){
add_option('widget_latestPosts' , $data);
} else {
update_option('widget_latestPosts' , $data);
}
}
function deactivate(){
delete_option('widget_latestPosts');
}
function control(){
$data = get_option('widget_latestPosts');
echo "<p><label>Title</label><br /><input name='widget_latestPosts_title' type='text' size='35' value='".$data['title']."' /></p>";
echo "<p><label>How many?</label><br /><input name='widget_latestPosts_size' type='text' size='3' value='".$data['size']."' /></p>";
if (isset($_POST['widget_latestPosts_size'])){
$data['title'] = attribute_escape($_POST['widget_latestPosts_title']);
$data['size'] = attribute_escape($_POST['widget_latestPosts_size']);
update_option('widget_latestPosts', $data);
}
}
function widget($args){
$data = get_option('widget_latestPosts');
echo $args['before_widget'];
echo $args['before_title'] .$data['title']. $args['after_title'];
$r = new WP_Query(array('showposts' => $data['size'], 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
if ($r->have_posts()) :
echo "<ul id=\"latestPosts\">";
while ($r->have_posts()) : $r->the_post(); ?>
<li><?php the_time('m/j/y'); ?> - <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endwhile;
echo "</ul>";
wp_reset_query(); // Restore global post data stomped by the_post().
endif;
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Latest Posts', array('Widget_latestPosts', 'widget'));
register_widget_control('Latest Posts', array('Widget_latestPosts', 'control'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment