WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent Posts Widget with Byline? (2 posts)

  1. Zooney
    Member
    Posted 4 years ago #

    One thing I'd really like is a simple Widget which would essentially be the Recent posts widget except that instead of only displaying a linked list of post titles it would also place a "by [author]" after the link.

    Something more like:

    I can see how to create a widget from scratch to do this, but, geez... should I have to?

    Does anyone know if it's possible to extend the class which creates the Recent Posts widget, or if there are filters available for the list items the widget spits out?

    Any help would be appreciated.

    It'd be great if there were more options, or simpler ways to modify the Recent Posts widget... It's silly that it seems that I'll have to reinvent the wheel to do something so trivial...

    Thanks in advance for any help on this...

  2. Zooney
    Member
    Posted 4 years ago #

    Hello me,

    I think you figured it out thanks to a kind person on WordPress's bug tracker.

    You can extend the Recent Posts widget (its class name is WP_Widget_Recent_Posts) like so:

    class RecentPostsWithByline_Widget extends WP_Widget_Recent_Posts {
    
    function RecentPostsWithByline_Widget () {
        $widget_ops = array('classname' => 'widget_recent_entries_with_byline', 'description' => __( "The most recent posts on your site with a byline added") );
        $this->WP_Widget('recent-posts-with-byline', __('Recent Posts with Byline'), $widget_ops);
        $this->alt_option_name = 'widget_recent_entries';
    
        add_action( 'save_post', array(&$this, 'flush_widget_cache') );
        add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
        add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
      }
    
      function widget( $args, $instance ) {
        $cache = wp_cache_get('widget_recent_posts', 'widget');
    
        if ( !is_array($cache) )
          $cache = array();
    
        if ( isset($cache[$args['widget_id']]) ) {
          echo $cache[$args['widget_id']];
          return;
        }
    
        ob_start();
        extract($args);
    
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
        if ( !$number = (int) $instance['number'] )
          $number = 10;
        else if ( $number < 1 )
          $number = 1;
        else if ( $number > 15 )
          $number = 15;
    
        $r = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
        if ($r->have_posts()) :
    ?>
        <?php echo $before_widget; ?>
        <?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <ul>
        <?php  while ($r->have_posts()) : $r->the_post(); ?>
        <li><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><span class="recent-posts-widget-byline"> by <?php the_author(); ?></span></li>
        <?php endwhile; ?>
        </ul>
        <?php echo $after_widget; ?>
    <?php
        // Reset the global $the_post as this query will have stomped on it
        wp_reset_postdata();
    
        endif;
    
        $cache[$args['widget_id']] = ob_get_flush();
        wp_cache_set('widget_recent_posts', $cache, 'widget');
      }
    }

    ...if you're doing so in a plugin, you'll have to include the default-widgets.php file. For instance, if your plugin is located in a directory in the plugins folder, you'll need something like this:

    include_once ( dirname(__FILE__) . '/../../../wp-includes/default-widgets.php' );

    ...in your plugin file before you attempt to extend the class (so that the WP_Widget_Recent_Posts class will be available and you don't get a Fatal error: Class 'WP_Widget_Recent_Posts' not found in...).

    I hope this helps someone out, me. If anything it keeps you from having to rewrite or copy the entire widget.

    ...that's... sort of useful, I guess.

Topic Closed

This topic has been closed to new replies.

About this Topic