Created
October 3, 2010 19:36
-
-
Save sorich87/608850 to your computer and use it in GitHub Desktop.
So's WordPress Recommended Posts Widget: displays a customizable list of recommended posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description: So's WordPress Recommended Posts Widget: displays a customizable list of recommended posts | |
* Author: Ulrich SOSSOU | |
* Author URI: http://ulrichsossou.com/ | |
* Version: 1.0 | |
* Licence: GPLv2 | |
* Compatibility: WordPress 3.0+ | |
* | |
*/ | |
/* | |
Copyright (C) 2010 Ulrich SOSSOU | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
class SO_Widget_Recommended_Posts extends WP_Widget { | |
function SO_Widget_Recommended_Posts() { | |
$widget_ops = array('classname' => 'widget_recommended_posts', 'description' => __('A customisable list of recommended posts')); | |
$this->WP_Widget('so-recommended-posts', __('Recommended Posts'), $widget_ops); | |
} | |
function sort_posts_by_input( $sortby, $thequery ) { | |
return $sortby = "FIND_IN_SET(ID, '" . implode( ',', $thequery->query['post__in'] ) . "')"; | |
} | |
function excerpt_length( $length ) { | |
return $length = 7; | |
} | |
function excerpt_more( $more ) { | |
return $more = ''; | |
} | |
function widget( $args, $instance ) { | |
extract($args); | |
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); | |
$posts = explode( ',', empty($instance['posts']) ? '' : $instance['posts'] ); | |
foreach( $posts as $key => $value ) { | |
$posts[$key] = trim( $value ); | |
} | |
add_filter( 'posts_orderby', array( &$this, 'sort_posts_by_input' ), 10, 2 ); | |
$posts_query = new WP_Query( array( 'post__in' => $posts, 'orderby' => 'post__in' ) ); | |
remove_filter( 'posts_orderby', array( &$this, 'sort_posts_by_input' ) ); | |
add_filter( 'excerpt_length', array( &$this, 'excerpt_length' ) ); | |
add_filter( 'excerpt_more', array( &$this, 'excerpt_more' ) ); | |
echo $before_widget; | |
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?> | |
<ul class="so-recommended-posts"> | |
<?php $i = 1; while ( $posts_query->have_posts() ) : $posts_query->the_post(); | |
echo '<li>'; | |
if( $i < 4 ) { | |
echo '<span class="post-thumb">' . get_the_post_thumbnail() . '</span><span class="post-rank">#' . $i . '</span><span class="post-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></span>'; | |
} else { | |
echo '<span class="post-rank">' . $i . '.</span><span class="post-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></span> -- <span class="post-desc">' . get_the_excerpt() . '</span>'; | |
} | |
echo '</li>'; | |
$i++; endwhile; ?> | |
</ul> | |
<?php | |
echo $after_widget; | |
remove_filter( 'excerpt_length', array( &$this, 'excerpt_length' ) ); | |
remove_filter( 'excerpt_more', array( &$this, 'excerpt_more' ) ); | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['posts'] = strip_tags($new_instance['posts']); | |
return $instance; | |
} | |
function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'posts' => '1, 2, 3, 4, 5, 6, 7, 8, 9, 10' ) ); | |
$title = strip_tags($instance['title']); | |
$posts = strip_tags($instance['posts']); | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> | |
<p><label for="<?php echo $this->get_field_id('posts'); ?>"><?php _e('Posts (enter a comma-separated list of posts IDs):'); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" type="text" value="<?php echo esc_attr($posts); ?>" /></p> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment