Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Exclude Cat with ID"4" from widget WordPress

  • SOLVED

Hi Guys,

My developer is away at the moment and I need to exclude a category from a news widget, problem is, all that I am trying is not working.

Here is the Widget's code:
/**
* News widget class
*
* @since 2.8.0
*/
class WP_am_News extends WP_Widget {

function WP_am_News() {
$widget_ops = array('classname' => '', 'description' => __('News widget'));
$this->WP_Widget('am_news', __('News'), $widget_ops, $control_ops);
}

function widget( $args, $instance ) {
global $post_number;
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$post_number = strip_tags($instance['number']);

if(empty($post_number))
$post_number = 3;
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title.$title.$after_title;} ?>
<?php require(TEMPLATEPATH.'/templates/sidebar_news.php'); ?>
<?php
echo $after_widget;
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = strip_tags($new_instance['number']);
return $instance;
}

function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'number' => 3) );
$title = strip_tags($instance['title']);
$number = strip_tags($instance['number']);
?>
<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('number'); ?>"><?php _e('Number:'); ?></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 esc_attr($number); ?>" /></p>
<?php
}
}

register_widget('WP_am_News');


All I need to do is to exclude the Category (ID=4) from displaying.

I DO NOT WANT TO USE A PLUGIN, as that seems to be a standard response here.

First person who gets it right wins :)

Thanks

Answers (1)

2011-06-19

Michael Fields answers:

Please post the code from templates/sidebar_news.php. This file should be located in your theme.


Justin Walmsley comments:

<?php
global $am_option, $post_number;
if(empty($post_number))
$post_number = 3;
?>
<?php query_posts('cat=&showposts='.$post_number); ?>
<?php if (have_posts()) : ?>
<dl class="latest-news">
<?php while (have_posts()) : the_post(); ?>
<dt><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></dt>
<dd><?php echo get_the_excerpt(); ?></dd>
<?php endwhile; ?>
</dl>
<p><a href="<?php echo get_option('home'); ?>/news/">&raquo; View all News Posts</a></p>
<?php endif; wp_reset_query(); ?>


Michael Fields comments:

Thanks! Changing the following line should do the trick.

<?php query_posts( 'cat=-4&showposts='.$post_number); ?>