WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently using this code to extend WP_Widget_Recent_Posts, but I would like to exclude the field that gives the user the ability to show how many posts they want. I have a set number in my custom query to 4, so this box is useless and I don't want to confuse the user.

class JA_Recent_Work_Widget extends WP_Widget_Recent_Posts {

function __construct() {
    $widget_ops = array('classname' => 'recent_work', 'description' => 'This widget will display 4 of your most recent works in the ART category Jay.');
    $this->WP_Widget('ja_recent_work', 'Jay Alders Recent Work', $widget_ops);

function widget($args, $instance) {

    extract( $args );

    if( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
                $number = 10;

    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
        $r = new WP_Query('category_name=art&posts_per_page=4');
        if ($r->have_posts()) :
        <?php echo $before_widget; ?>
        <?php if ($title) echo $before_title . $title . $after_title; ?>
        <?php while ($r->have_posts()) : $r->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title()); ?>"><?php echo the_title(); ?></a>
        <a href="<?php the_permalink() ?>"><?php echo the_post_thumbnail(); ?></li>
        <?php endwhile; ?>
        <?php echo $after_widget ?>


 function my_recent_widget_registration() {
   add_action('widgets_init', 'my_recent_widget_registration');
share|improve this question

You need to add a 'form' method. That is the method that creates the widget form on the backend. In the current default widget, that method looks like this:

function form( $instance ) {
  $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  <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><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
  <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>

Copy that into your class and remove the line starting with $number and the form field that uses it-- the second <p>.

Also, in your widget method, you can skip the conditional-- if( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )-- and just set $number. You don't need to check to see if `$instance['number'] is set. It won't be.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.