Home » Wordpress » Recent posts wordpress widget, with limited text and 'read more' link

Recent posts wordpress widget, with limited text and 'read more' link

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am creating a WordPress Recent Posts widget for practice. However I have run into a few things that I just don’t know how to do. First of all the widget has to display the date and time of the post, the posts title, text with a smart cut off character limit and a read more link. It also has to allow the admin to specify the title, the category, the number of posts to display, number of characters to display on the post, and lastly change the wording of the ‘read more’ link.

I have already created the widget and figured out how to do everything but make the category selection a drop down selection with the categories already in there (right now it is just a text block for the admin that you can type in which category to display), how to limit characters in the_excerpt while making it so it smartly cuts off at the end of a word and allowing admin to specify how my characters are allowed, and how to create a read more link while allowing admin to specify what it says.

I will post the code I have so far below along with a link to the site that contains the widget in the sidebar. I am new to WordPress Widget design so help would be greatly appreciated.

<?php
/*
Plugin Name: News Recent Posts Widget
Plugin URI: 
Description: A recent post widget with extra functions for client management
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/


class recentpost extends WP_Widget {

    public function __construct() {
    parent::WP_Widget(
    // or parent::__construct(
        false, 
        'Kevin - Recent Posts Widget',
        array(
            'description' => __('A recent post widget with extra functions for client management') 

        )
    );
    ;
}

    public function widget( $args, $instance ) {


        extract( $args );

        $headline = $instance['headline'];
        $category = $instance['category'];
        $numberposts = $instance['numberposts'];
        $readmore = $instance['readmore'];


        echo $before_widget;

        echo $before_title;

        echo "<p class=\"headline\">$headline</p>";

        echo $after_title;

        $args = array( 'numberposts' => $numberposts, 'category_name' => $category );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
        setup_postdata(get_post($recent['ID']));
        echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' .   get_the_title().'</a> ';
        echo get_the_time('F j, Y', $recent['ID']);
        the_excerpt();
}
wp_reset_postdata();


        echo $after_widget;


    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['category'] = ( $new_instance['category'] );
        $instance['numberposts'] = ( $new_instance['numberposts'] );
        $instance['readmore'] = ( $new_instance['readmore'] );
        return $instance;
    }


    public function form( $instance ) {

        $headline = $instance[ 'headline' ];
        $category = $instance[ 'category' ];
        $numberposts = $instance[ 'numberposts' ];
        $readmore = $instance[ 'readmore' ];

        ?>

<p>
  <label for="<?php echo $this->get_field_id( 'headline' ); ?>">
    <?php _e( 'Headline:' ); ?>
  </label>
  <input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">
  <?php _e( 'Category:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">
  <?php _e( 'Number of posts:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" type="text" value="<?php echo esc_attr( $numberposts ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'readmore' ); ?>">
  <?php _e( 'Read More:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'readmore' ); ?>" name="<?php echo $this->get_field_name( 'readmore' ); ?>" type="text" value="<?php echo esc_attr( $readmore ); ?>" />
</p>
<?php 
    }
}

add_action( 'widgets_init', create_function('', 'return register_widget("recentpost");') );


?>

http://www.modmacro.us/wpsandbox/

Answers:

A lot to this question. I think it’s great you’re tackling this as a practice exercise. That’s exactly how I learned. I can help with a few parts.

categories

In the form function, first you need to get a list of all the categories using WordPress’ get_categories function.

$categories = get_categories(array('type'=>'post','orderby'=> 'name','order'=> 'ASC'));

Then, to display a drop-down menu in the form, you use a for-loop to go through each category.

echo '<select name="' . $this->get_field_name('category') . '" id="' . $this->get_field_id('category') . '">';
  foreach($categories as $category):
    echo '  <option value="' . $category->slug .'" '. selected($category->slug, $instance['category'], false) . '>' . $category->name . '</option>';        
  endforeach;
echo '</select>';

Show posts date/time, etc

I think it’s better to use the WP_Query class, which is more flexible and seems to be the preferred route these days. In your widget function, it would look something like this

$args = array( 'numberposts' => $numberposts, 'category_name' => $category );

$myquery = new WP_Query($args);

while($myquery->have_posts()): $myquery->the_post();

  echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' .   get_the_title().'</a> ';
  echo get_the_time('F j, Y', $myquery->ID);
  the_excerpt();

endwhile;
wp_reset_query();
wp_reset_postdata();

Limit Excerpt

This will apply to all uses of excerpts in your theme however.

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');