
WordPress: Optimize the Recent Post Widget – Exclude Categories
Posted in TUTORIALS by TopherI was trying to find a way to exclude categories from the Genesis theme ‘Featured Post’ widget and got a bit sidetracked and ended up finding this useful piece of code for the ‘Recent Post’ widget. Not what I wanted but I’m sure will come in handy! I hate to say I can’t remeber where I found this so if you know or if you wrote this piece of code, first thanks and second let me know and I will give your full credit!
So the problem:
You want to add a list of Recent post to your blog. You have many ‘categories’. You only want to display certain categories in the feed?
As it stands the standard ‘Recent Post’ widget is very simple and does not let you exclude categories. So that’s what we have here!
(you will need FTP access and a functions.php file in your theme/child theme to make use of this code snippet.)
- First go into your themes files and open your functions.php file
- copy and paste this code snippet into the functions.php file
- Save
- go to your Widgets area and there will be a new widget called: ‘Recent Posts with Exclude’
- done! Enjoy!
/** * Recent_Posts widget w/ category exclude class * This allows specific Category IDs to be removed from the Sidebar Recent Posts list * */ class WP_Widget_Recent_Posts_Exclude extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") ); parent::__construct('recent-posts', __('Recent Posts with Exclude'), $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( $args['widget_id'] ) ) $args['widget_id'] = $this->id; 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 ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) $number = 10; $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'category__not_in' => explode(',', $exclude) )); if ($r->have_posts()) : ?> <?php //echo print_r(explode(',', $exclude)); ?> <?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></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'); } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; $instance['exclude'] = strip_tags( $new_instance['exclude'] ); $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['widget_recent_entries']) ) delete_option('widget_recent_entries'); return $instance; } function flush_widget_cache() { wp_cache_delete('widget_recent_posts', 'widget'); } function form( $instance ) { $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $number = isset($instance['number']) ? absint($instance['number']) : 5; $exclude = esc_attr( $instance['exclude'] ); ?> <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 $title; ?>" /></p> <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> <p> <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude Category(s):' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" /> <br /> <small><?php _e( 'Category IDs, separated by commas.' ); ?></small> </p> <?php } } function WP_Widget_Recent_Posts_Exclude_init() { unregister_widget('WP_Widget_Recent_Posts'); register_widget('WP_Widget_Recent_Posts_Exclude'); } add_action('widgets_init', 'WP_Widget_Recent_Posts_Exclude_init'); ?>
Related Posts
About Author
I'm the curator here at designshifts.com as well as being a web developer by day. I have a passion for design and love to share inspiration and tips and tricks with the community.
Hi, i’m getting this error when I copy and paste it into functions.php
”
Parse error: syntax error, unexpected ‘=’, expecting ‘)’ in /var/sites/r/ryancurtismusic.com/public_html/wp-includes/functions.php on line 3878″
Any reason why this would be?
I might be able to help with the attribution — It looks as though this snippet was posted to Pastebin by WP forums user “lawless.” Quite a helpful piece of code!
@ryan, or anyone else who copies and pastes from this — HTML tag brackets () in the code snippet above have been converted to their corresponding entity tags (e.g.
<
), so directly copying and pasting this content will result in syntax errors. See the pastebin link above, or convert the bracket entities back to real angle brackets, and that should work correctly.To clarify, because I messed up the formatting: Where the code appears above as
<
, you want to change it to<
, and vice-versa with>
to>
I have fixed up the code so….
'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
parent::__construct('recent-posts', __('Recent Posts with Exclude'), $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( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
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 ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
$number = 10;
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
$r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'category__not_in' => explode(',', $exclude) ));
if ($r->have_posts()) :
?>
have_posts()) : $r->the_post(); ?>
<a href="" title="">
flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$number = isset($instance['number']) ? absint($instance['number']) : 5;
$exclude = esc_attr( $instance['exclude'] );
?>
<label for="get_field_id('title'); ?>">
<input class="widefat" id="get_field_id('title'); ?>" name="get_field_name('title'); ?>" type="text" value="" />
<label for="get_field_id('number'); ?>">
<input id="get_field_id('number'); ?>" name="get_field_name('number'); ?>" type="text" value="" size="3" />
<label for="get_field_id('exclude'); ?>"> <input type="text" value="" name="get_field_name('exclude'); ?>" id="get_field_id('exclude'); ?>" class="widefat" />
of course like your web site but you need to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling problems and I to find it very bothersome to tell the truth nevertheless I’ll certainly come back again.
Yup I do appologies, not my strong suit! I will try and go back and take a look when I have more time. Thanks for the feedback, I can take it 😉