Suporte » Plugins » Acrescentar opção de selecionar categoria em wigget de posts recentes

  • Resolvido luisclaudio

    (@luisclaudio)


    Pessoal, estou editando um tema que tem um widget para mostrar os posts mais recentes, com miniaturas.
    Porém gostaria de poder selecionar apenas uma categoria para que fossem exibidos os posts na sidebar.
    Já tentei modificar mas não tive êxite.
    Agradeço se alguém puder auxiliar.

    O código é o seguinte:

    #-----------------------------------------------------------------
    # Recent Post Widget
    #-----------------------------------------------------------------
    
    // Recent Post Class
    //................................................................
    class theme_recentPost_widget extends WP_Widget {
    
        function theme_recentPost_widget() {
    		global $themeTitle;
    		$options = array('classname' => 'recentPosts-widget', 'description' => __( "Theme styled recent posts with optional preview image.") );
    		$controls = array('width' => 250, 'height' => 200);
    		$this->WP_Widget('recentposts', __($themeTitle.' - Recent Post'), $options, $controls);
        }
    
        function widget($args, $instance) {
    		global $wpdb, $shortname;
            extract( $args );
    		$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
    
    		// Sub-Title
    		if ( !empty($instance['subtitle']) ) {
    			$subtitle = '<span>'. stripslashes($instance['subtitle']) .'</span>';
    		}
    
    		// number of posts to show
    		if ( !$number = (int) $instance['number'] ) {
    			$number = 3;	// default
    		}else if ( $number < 1 ) {
    			$number = 1;	// minimum
    		}else if ( $number > 15 ) {
    			$number = 15;	// maximum
    		}
    
    		// length of content description
    		if ( !$excerpt = (int) $instance['excerpt'] ) {
    			$excerpt = 12;	// default
    		}else if ( $excerpt < 0 ) {
    			$excerpt = 0;	// minimum
    		}else if ( $excerpt > 55 ) {
    			$excerpt = 55;	// maximum
    		}
    
    		$disable_thumb = $instance['disable_thumb'] ? '1' : '0';
    
    		// setup post query
    		$posts = get_posts("numberposts=$number&offset=0");
    
    		echo $before_widget;
    		echo $before_title . $title . $subtitle . $after_title;
    
    		if($posts){ ?>
    		<ul class="post-list">
    			<?php
    			foreach($posts as $post){
    				setup_postdata($post);
    				$post_title = stripslashes($post->post_title);
    				$permalink = get_permalink($post->ID);
    				$post_image = showImage(64, 64, $post_title, 'small', $post);
    				?>
    			<li>
    				<?php if (!$disable_thumb) { ?>
    					<a href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>" class="post-listImage"><?php echo $post_image['full']; ?></a>
    				<?php } ?>
    				<a href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>" class="post-listTitle"><?php echo $post_title; ?></a>
    				<p><?php echo customExcerpt(get_the_content(), $excerpt); ?></p>
    				<div class="clear"></div>
    			</li>
    				<?php
    			} // end foreach ?>
    		</ul>
    			<?php
    		} // end IF ($posts)
    
    		echo $after_widget;
        }
    
        function update($new_instance, $old_instance) {
            $instance = $old_instance;
    		$instance['title'] = strip_tags($new_instance['title']);
    		$instance['subtitle'] = strip_tags($new_instance['subtitle']);
    		$instance['number'] = (int) $new_instance['number'];
    		$instance['excerpt'] = (int) $new_instance['excerpt'];
    		$instance['disable_thumb'] = !empty($new_instance['disable_thumb']) ? 1 : 0;
    
            return $instance;
        }
    
        function form($instance) {
    		$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
    		$subtitle = isset($instance['subtitle']) ? esc_attr($instance['subtitle']) : '';
    		$disable_thumb = isset( $instance['disable_thumb'] ) ? (bool) $instance['disable_thumb'] : false;
    		if ( !isset($instance['number']) || !$number = (int) $instance['number'] ) {
    			$number = 3;	// set default
    		}
    		if ( !isset($instance['excerpt']) || !$excerpt = (int) $instance['excerpt'] ) {
    			$excerpt = 12;	// set default
    		}
            ?>
    
    		<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('subtitle'); ?>"><?php _e('Sub-Title:'); ?></label>
    			<input class="widefat" id="<?php echo $this->get_field_id('subtitle'); ?>" name="<?php echo $this->get_field_name('subtitle'); ?>" type="text" value="<?php echo $subtitle; ?>" />
    		</p>
    		<p>
    			<label for="<?php echo $this->get_field_id('number'); ?>">Number of recent posts to display:</label>
    			<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
    		</p>
    		<p>
    			<label for="<?php echo $this->get_field_id('excerpt'); ?>">Length of post excerpt:</label>
    			<input class="widefat" id="<?php echo $this->get_field_id('excerpt'); ?>" name="<?php echo $this->get_field_name('excerpt'); ?>" type="text" value="<?php echo $excerpt; ?>" />
    			<small>(0 for no excerpt)</small>
    		</p>
    		<p>
    			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('disable_thumb'); ?>" name="<?php echo $this->get_field_name('disable_thumb'); ?>"<?php checked( $disable_thumb ); ?> />
    			<label for="<?php echo $this->get_field_id('disable_thumb'); ?>"><?php _e( 'Hide Thumbnail?' ); ?></label>
    		</p>
            <?php
        }
    
    }
Visualizando 2 respostas - 1 até 2 (de um total de 2)
  • Eduardo

    (@eduardozulian)

    Luis, o ideal seria que você criasse um novo widget que fizesse isso pra você. Eu acabei escrevendo um widget baseado no Recent Posts que apenas adiciona uma gategoria. Dá uma olhada no código que acho que vai te servir!

    Criador do tópico luisclaudio

    (@luisclaudio)

    Eduardo, obrigado pela resposta, farei um teste em seu código assim que possível.
    Por enquanto usei o plugin Flexible Posts Widget, que também tem essa opção, entre outras.
    Valeu!

Visualizando 2 respostas - 1 até 2 (de um total de 2)
  • O tópico ‘Acrescentar opção de selecionar categoria em wigget de posts recentes’ está fechado para novas respostas.