Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Recent Posts with Thumbnail
===========================

This is a simple WordPress plugin that creates a recent posts widget that displays the post thumbnail and, optionally, the post title and date. You can select the number of posts to display and choose to hide or show the title and date.
This is a simple WordPress plugin that creates a recent posts widget that displays the post thumbnail and, optionally, the post title and date. You can select the number of posts to display and choose to hide or show the title, date, and "read more" text. You may also restrict the posts displayed to a particular category.

Very little styling has been applied so that the theme designer, developer or use can easily customize the appearance.

Expand Down Expand Up @@ -59,6 +59,8 @@ Finally, you can select the size of thumbnail to display. Valid values include "

Changelog
------------

* 0.7 Added ability to filter by category
* 0.6 Added "read more" text option
* 0.5 Fixed error causing widget not to save (hat tip [@blakewatson](https://twitter.com/blakewatson))
* 0.4 Fixed syntax error (hat tip [@clintonwilmott](http://twitter.com/clintonwilmott))
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Stable tag: trunk
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Display a select number of recent posts with thumbnail and, optionally, post title and date via widget or shortcode.
Display a select number of recent posts with thumbnail and, optionally, post title and date via widget or shortcode. Optionally restrict the posts displayed to a particular category.

== Description ==

Expand Down Expand Up @@ -87,6 +87,9 @@ You can do that with the shortcode, too, like so: `<?php echo do_shortcode('[nea

== Changelog ==

= 0.6 =
* Added ability to filter by category

= 0.5 =
* Added read more text option

Expand Down
52 changes: 45 additions & 7 deletions recent-posts-with-thumbnail-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Recent Posts with Thumbnails Widget
Plugin URI: https://github.com/zoerooney/Recent-Posts-Thumbnail-Widget
Description: Creates a widget that displays recent posts in a nice, easy to style layout.
Version: 0.4
Version: 0.6
Author: Zoe Rooney
Author URI: http://zoerooney.com
License: GPL2
Expand Down Expand Up @@ -45,6 +45,7 @@ public function widget($args, $instance) {

$title = apply_filters( 'widget_title', $instance['title'] );
$number = $instance['number'];
$category = $instance['category'];
$thumbsize = $instance['thumbsize'];
$show_title = isset($instance['show_title']) ? $instance['show_title'] : true;
$show_date = isset($instance['show_date']) ? $instance['show_date'] : true;
Expand All @@ -63,9 +64,14 @@ public function widget($args, $instance) {
}
echo $after_title; // widget title

$args = array (
$args = array(
'posts_per_page' => $number,
);

if ( isset( $category )) {
$args['cat'] = $category;
}

$neatly_posts = new WP_Query($args);
if( $neatly_posts->have_posts() ) {
echo '<ul>';
Expand Down Expand Up @@ -96,6 +102,9 @@ public function form( $instance ) {
if (isset( $instance[ 'number' ] ) ) {
$number = $instance['number'];
} else { $number = '5'; }
if (isset( $instance[ 'category' ] ) ) {
$category = $instance['category'];
} else { $category = null; }
if (isset( $instance[ 'thumbsize' ] ) ) {
$thumbsize = $instance['thumbsize'];
} else { $thumbsize = 'thumbnail'; }
Expand All @@ -116,13 +125,34 @@ public function form( $instance ) {

<p style="border-bottom:4px double #eee;padding: 0 0 10px;">
<label for="<?php echo $this->get_field_id( 'number' ); ?>">Number of Posts Displayed</label>
<input id="<?php echo $this->get_field_id( 'number'); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr($number); ?>" type="number" style="width:100%;" /><br>
<input id="<?php echo $this->get_field_id( 'number'); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr($number); ?>" type="number" style="width:100%;" /><br><br>

<label for="<?php echo $this->get_field_id( 'category' ); ?>">Display Posts from Category</label>
<?php


$field_id = $this->get_field_id( 'category');
$name = $this->get_field_name( 'category' );
$selected = esc_attr($category);
$args = array(
'id' => $field_id,
'show_option_all' => 'All',
'name' => $name,
'selected' => $selected
);
wp_dropdown_categories($args);

?><br><br>

<label for="<?php echo $this->get_field_id( 'thumbsize' ); ?>">Thumbnail Size</label>
<input id="<?php echo $this->get_field_id( 'thumbsize'); ?>" name="<?php echo $this->get_field_name( 'thumbsize' ); ?>" value="<?php echo esc_attr($thumbsize); ?>" style="width:100%;" /><br><br>

<label for="<?php echo $this->get_field_id( 'show_title' ); ?>">Show the post titles?
<input id="<?php echo $this->get_field_id( 'show_title'); ?>" name="<?php echo $this->get_field_name( 'show_title' ); ?>" <?php checked($instance['show_title'], true) ?> type="checkbox" /></label><br><br>

<label for="<?php echo $this->get_field_id( 'show_date' ); ?>">Show the post dates?
<input id="<?php echo $this->get_field_id( 'show_date'); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked($instance['show_date'], true) ?> type="checkbox" /></label><br><br>

<label for="<?php echo $this->get_field_id( 'show_read_more' ); ?>">Show "read more" text for each post?
<input id="<?php echo $this->get_field_id( 'show_read_more'); ?>" name="<?php echo $this->get_field_name( 'show_read_more' ); ?>" <?php checked($instance['show_read_more'], true) ?> type="checkbox" /></label>
</p>
Expand All @@ -137,6 +167,7 @@ public function update( $new_instance, $old_instance ) {

$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = strip_tags($new_instance['number']);
$instance['category'] = strip_tags($new_instance['category']);
$instance['thumbsize'] = strip_tags($new_instance['thumbsize']);
$instance['show_title'] = $new_instance['show_title'] ? 1 : 0;
$instance['show_date'] = $new_instance['show_date'] ? 1 : 0;
Expand All @@ -161,6 +192,7 @@ function neatly_recent_posts_shortcode( $atts ) {
extract( shortcode_atts( array(
'title_text' => 'Recent Posts',
'number_posts' => '3',
'category' => '',
'hide_title' => 'false',
'hide_date' => 'false',
'hide_read_more' => 'false',
Expand All @@ -169,9 +201,16 @@ function neatly_recent_posts_shortcode( $atts ) {

ob_start();

$neatly_loop = new WP_Query( array(
'posts_per_page' => $number_posts
));
$args = array(
'posts_per_page' => $number,
);

if ( isset( $category )) {
$args['cat'] = $category;
}

$neatly_loop = new WP_Query($args);

echo '<div class="neatly-recent neatly-recent-shortcode"><h3>' . $title_text . '</h3>';
if ( $neatly_loop->have_posts() ) : ?>
<ul>
Expand All @@ -195,7 +234,6 @@ function neatly_recent_posts_shortcode( $atts ) {
ob_end_clean();
return $content;
}

add_shortcode( 'neatly_recent', 'neatly_recent_posts_shortcode' );

?>