Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wrote a widget that has multiple output format based on type dropdown list. I intend to use this widget on both left and right sidebars. The problem however is it only shows up on one sidebar either left or right. I want the widget to appear on both sidebars please find the widget code below:

 class Posts_Widgets extends WP_Widget{

  function __construct(){
    $id = "posts_widgets";
    $name = "Anzima Posts Widgets";
    $widgets_ops = array('description'=>__('Widget for displaying various posts formats'));
    $control_ops = array('width' => 400, 'height' => 350);
    parent::__construct($id,$name,$widgets_ops);
  }

  function form($instance){
    $title = esc_attr($instance['title']);
    $cats  = esc_attr($instance['cats']);
    $type  = esc_attr($instance['type']);
    ?>
         <p>
            <label for="<?php echo $this->get_field_id('title'); ?>" ><?php _e('Title'); ?>:
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" 
              name="<?php echo $this->get_field_name('title');?>" value="<?php echo esc_attr($title);?>" />
            </label>
         </p>

         <p>
            <label for="<?php echo $this->get_field_id('cats'); ?>" ><?php _e('Post Categories'); ?>:
            <select class="widefat" id="<?php echo $this->get_field_id('cats'); ?>" name="<?php echo $this->get_field_name('cats')?>" >
            <?php $cats_a = $this->get_category(); ?>
            <option value="" ><?php _e('Please select an option'); ?></option>
            <option value="x" <?php if($cats == 'x'){?>selected="selected"<?php }?> ><?php _e('Most Read'); ?></option>
            <?php foreach($cats_a as $o){ ?>
                <option <?php if($cats == $o->id){?>selected="selected"<?php }?> value="<?php echo $o->id; ?>"><?php echo $o->name; ?></option>
            <?php } ?>
            </select>
            </label>
         </p>

         <p>
            <label for="<?php echo $this->get_field_id('type'); ?>" ><?php _e('Display Type'); ?>:
            <select class="widefat" id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type')?>" >
               <option value="" ><?php _e('Please select an option'); ?></option>
               <option <?php if($type == 1){?>selected="selected"<?php }?> value="1" ><?php echo _e('Main Plus Thumbs List'); ?></option>
               <option <?php if($type == 2){?>selected="selected"<?php }?> value="2" ><?php echo _e('Item List'); ?></option>
               <option <?php if($type == 3){?>selected="selected"<?php }?> value="3" ><?php echo _e('Main Plus List'); ?></option>
               <option <?php if($type == 4){?>selected="selected"<?php }?> value="4" ><?php echo _e('Grid'); ?></option>
               <option <?php if($type == 5){?>selected="selected"<?php }?> value="4" ><?php echo _e('Single Post'); ?></option>
            </select>
            </label>
         </p>
    <?php 
  }

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

  function widget($args, $instance) {
      extract($args, EXTR_SKIP);
      $data = array();
      if($instance['cats'] != 'x'){
        $data['title'] = get_cat_name($instance['cats']);
        $data['image'] = ((is_file(TEMPLATEPATH . '/images/'.$instance['cats'].'.png'))?get_bloginfo('stylesheet_directory') . '/images/'.$instance['cats'].'.png':'');
      }else{
        $data['title'] = 'most read';
        $data['image'] = get_bloginfo('stylesheet_directory') . '/images/most-read.png';
      }
      $data['id'] = $instance['cats'];
      $data['list'] = $this->get_list($instance['cats'],$instance['type']);
      include_once TEMPLATEPATH . '/widgets/templates/'.$instance['type'].'.php';
  }
  }
?>

Thank you in advance for your utterly appreciated help


FlashingCursor; I am registering the widget just forgot to paste it

function reg_post_widget(){
register_widget( 'Posts_Widgets' );
}
add_action( 'widgets_init','reg_post_widget');

The problem is it appears only on onside bar but not both although in the backend I can drop the widget on both sidebars


Thank you for your response, I am registering my widget but forgot to paste it The problem however, is that the widget only appears on one sidebar either left or right but not both at a time. I don't know if I am missing some configuration option to make my widget multi-widget. Definitely I am missing something out!!! by the way how can I reply to answers instead of editing them??

share|improve this question
    
You ended up with two different accounts on site, I merged them. You should now be able to edit your question and leave comments on answers for it ("add comment" below them). –  Rarst Aug 16 '12 at 15:04

1 Answer 1

up vote 0 down vote accepted

I figured it out after looking at wp_option tables for my widget and the sidebars_widget option I figured out that my widget name has to be none white space separated, in my case it should be anzima_posts_widgets, also I have to include the base-id in the control_ops parameter as the first index.

Another issue I figured out that made look silly!!! is that I am having include_once for my html template files.

I am really glad that I figured it out

share|improve this answer

Your Answer

 
discard

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.