Skip to content

Instantly share code, notes, and snippets.

@eduardozulian
Last active December 14, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardozulian/5041604 to your computer and use it in GitHub Desktop.
Save eduardozulian/5041604 to your computer and use it in GitHub Desktop.
Widget that displays the latest WordPress posts from a certain category / Widget que mostra os últimos posts de uma determinada categoria
<?php
/**
* Cria o Widget que mostra os posts da categoria selecionada
*
*/
class Recent_Category_Posts extends WP_Widget {
/**
* Constructor
*
* @return void
**/
function Recent_Category_Posts() {
$widget_ops = array( 'classname' => 'widget_recent_category_posts', 'description' => __( 'Displays the latest posts from a certain category', '' ) );
$this->WP_Widget( 'widget_recent_category_posts', __( 'Recent Category Posts', '' ), $widget_ops );
$this->alt_option_name = 'widget_recent_category_posts';
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' ) );
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it's output
**/
function widget( $args, $instance ) {
$cache = wp_cache_get( 'widget_recent_category_posts', 'widget' );
if ( !is_array( $cache ) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = null;
if ( isset( $cache[$args['widget_id']] ) ) {
echo $cache[$args['widget_id']];
return;
}
ob_start();
extract( $args, EXTR_SKIP );
// Category ID
$category = (int) $instance['category'];
// Não havendo título, usamos o nome da categoria
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? get_category( $category )->name : $instance['title'], $instance, $this->id_base );
// Número de posts
if ( ! $number = absint( $instance['number'] ) )
$number = 5;
$category_args = array(
'cat' => $category,
'posts_per_page' => $number,
'post_status' => 'publish',
'ignore_sticky_posts' => true
);
$category_posts = new WP_Query( $category_args );
if ( $category_posts->have_posts() ) :
echo $before_widget;
echo $before_title;
echo '<a href="' . get_category_link( $category ) . '" title="' . sprintf( 'View all posts filed under %s', get_category( $category )->name ) . '">' . $title . '</a>';
echo $after_title;
echo '<ul>';
while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php
endwhile;
echo '</ul>';
// Final do widget
echo $after_widget;
// Reinicia o postdata
wp_reset_postdata();
endif; // if ( have_posts() )
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set( 'widget_recent_category_posts', $cache, 'widget' );
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
**/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['category'] = (int)( $new_instance['category'] );
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['widget_recent_category_posts'] ) )
delete_option( 'widget_recent_category_posts' );
return $instance;
}
function flush_widget_cache() {
wp_cache_delete( 'widget_recent_category_posts', 'widget' );
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
**/
function form( $instance ) {
$title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
$category = isset( $instance['category'] ) ? (int) $instance['category'] : 0;
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
?>
<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', '' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Category:', '' ); ?></label>
<?php wp_dropdown_categories( 'name='.$this->get_field_name( 'category' ) . '&id=' . $this->get_field_id( 'category' ) . '&show_count=1&class=widefat&selected=' . $category );?></p>
<p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', '' ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
<?php
}
}
/**
* Registra os widgets personalizados do tema
*
*/
function my_register_widgets() {
register_widget( 'Recent_Category_Posts' );
}
add_action( 'widgets_init', 'my_register_widgets' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment