Latest posts by current author widget plugin for Wordpress 3+


/ Published in: PHP
Save to your folder(s)

Displays latest posts by current author (place it within the loop). Adapted from code from [here](http://www.wpbeginner.com/wp-tutorials/how-to-display-related-posts-by-same-author-in-wordpress/) and a tutorial [here](http://www.makeuseof.com/tag/how-to-create-wordpress-widgets/)


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Plugin Name: Latest posts by current author
  4. Plugin URI: http://podojdi.ru/
  5. Description: Displays latest posts by current author (place it within the loop). Adapted from code from <a target="_blank" href="http://www.wpbeginner.com/wp-tutorials/how-to-display-related-posts-by-same-author-in-wordpress/">here</a> and a <a href="http://www.makeuseof.com/tag/how-to-create-wordpress-widgets/" target="_blank">tutorial here</a>
  6. Author: Alexander Belyaev
  7. Version: 1.0
  8. Author URI: http://podojdi.ru/
  9. */
  10.  
  11.  
  12. class LatestByAuthorWidget extends WP_Widget
  13. {
  14. function LatestByAuthorWidget()
  15. {
  16. $widget_ops = array('classname' => 'LatestByAuthorWidget', 'description' => 'Displays latest posts by current author' );
  17. $this->WP_Widget('LatestByAuthorWidget', 'Latest posts by current author', $widget_ops);
  18. }
  19.  
  20. function form($instance)
  21. {
  22. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  23. $title = $instance['title'];
  24. ?>
  25. <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
  26. <?php
  27. }
  28.  
  29. function update($new_instance, $old_instance)
  30. {
  31. $instance = $old_instance;
  32. $instance['title'] = $new_instance['title'];
  33. return $instance;
  34. }
  35.  
  36. function widget($args, $instance)
  37. {
  38. extract($args, EXTR_SKIP);
  39.  
  40. echo $before_widget;
  41. $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  42.  
  43. if (!empty($title))
  44. echo $before_title . $title . $after_title;
  45.  
  46. // WIDGET CODE GOES HERE
  47.  
  48.  
  49.  
  50. global $authordata, $post;
  51.  
  52. $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 7 ) );
  53. if ($authors_posts) {
  54. $output = '<ul>';
  55. foreach ( $authors_posts as $authors_post ) {
  56. $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
  57. }
  58. $output .= '</ul>';
  59. echo $output;
  60. }
  61. else {echo '<div class="notice">L\'autore ha ancora pubblicato solo questo post.</div>';};
  62.  
  63.  
  64. echo $after_widget;
  65. }
  66.  
  67. }
  68. add_action( 'widgets_init', create_function('', 'return register_widget("LatestByAuthorWidget");') );?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.