Created
October 3, 2010 15:58
-
-
Save sorich87/608684 to your computer and use it in GitHub Desktop.
So's WordPress Featured Categories Widget: displays a customized list of featured categories
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 Featured Categories Widget: displays a customized list of featured categories | |
* 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_Featured_Categories extends WP_Widget { | |
function SO_Widget_Featured_Categories() { | |
$widget_ops = array('classname' => 'widget_featured_categories', 'description' => __( 'A customizable list of featured categories', 'so' )); | |
$this->WP_Widget('so-featured-categories', __( 'Featured Categories', 'so' ), $widget_ops); | |
} | |
function widget( $args, $instance ) { | |
extract($args); | |
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); | |
$categories = explode( ',', empty($instance['categories']) ? '' : $instance['categories'] ); | |
foreach( $categories as $key => $value ) { | |
$categories[$key] = trim( $value ); | |
} | |
echo $before_widget; | |
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?> | |
<ul class="so-featured-categories"> | |
<?php | |
foreach( $categories as $category ) { | |
$category = get_category_by_slug($category); | |
if( $category ) { | |
$rand_post = new WP_Query( array( 'posts_per_page' => 1, 'orderby' => 'rand', 'cat' => $category->term_id ) ); | |
$rand_post = $rand_post->get_posts(); | |
$rand_post = $rand_post[0]; | |
echo '<li> | |
<span class="cat-thumb">' . get_the_post_thumbnail( $rand_post->ID ) . '</span> | |
<span class="cat-title"><a href="">' . $category->name . '</a></span> | |
<span class="cat-desc">' . $category->description . '</span> | |
<span class="cat-count">' . __( 'Posts', 'so' ) . ': ' . $category->count . '</span> | |
</li>'; | |
} | |
} | |
?> | |
</ul> | |
<?php | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['categories'] = strip_tags($new_instance['categories']); | |
return $instance; | |
} | |
function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'categories' => 'uncategorized, featured' ) ); | |
$title = strip_tags($instance['title']); | |
$categories = strip_tags($instance['categories']); | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'so' ); ?></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('categories'); ?>"><?php _e( 'Categories (enter a comma-separated list of categories slugs):', 'so' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" type="text" value="<?php echo esc_attr($categories); ?>" /></p> | |
<?php | |
} | |
} |
It should work fine with the latest version of WordPress.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to add something like this on my site http://www.makeityourring.biz/ Are there any updates available or is it still working with the latest wordpress releases?