Ciao a tutti, sto scrivendo un plugin per wordpress per recuperare i post recenti, basandomi su un tutorial che ho trovato in rete, la differenza che volevo aggiungere è che anziché avere una lista delle categorie da aggiungere/escludere, ho inserito un campo dove mettere gli ids delle categorie, così da non avere problemi con le categorie tassonomiche, o le categorie dei linguaggi. Il plugin funziona, ma solo se metto manualmente gli ids nel codice, non so come mai i dati inseriti nel campo non risultano.
Posto il codice di seguito.
Codice PHP:
class Ricky_Recent_Posts extends WP_Widget {

  function Ricky_Recent_Posts() {
     /* Widget settings. */
    $widget_ops = array(
      'classname' => 'postsfromcat',
      'description' => 'Allows you to display a list of recent posts from categories.');

     /* Widget control settings. */
    $control_ops = array(
       'width' => 250,
       'height' => 250,
       'id_base' => 'postsfromcat-widget');

    /* Create the widget. */
   $this->WP_Widget('postsfromcat-widget', 'Ricky Recent Posts', $widget_ops, $control_ops );
  }

  function form ($instance) {
    /* Set up some default widget settings. ,'rss'=>''*/
    $defaults = array('numberposts' => '5','catid'=>'0','title'=>'','excluded'=>'mille');
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>

  


    <label for="<?php echo $this->get_field_id('title'); ?>">Titolo:</label>
    <input type="text" name="<?php echo $this->get_field_name('title'?>" id="<?php echo $this->get_field_id('title'?> " value="<?php echo $instance['title'?>" size="20">
  </p>

  


   <label for="<?php echo $this->get_field_id('excluded'); ?>">Escludi Categoria ID:</label>
     
   <input type="text" name="<?php echo $this->get_field_id('excluded'); ?>" id="<?php echo $this->get_field_id('excluded'); ?>" value="<?php echo $instance['excluded'?>" size="20">
  </p>
  
  


   <label for="<?php echo $this->get_field_id('numberposts'); ?>">Numero di post:</label>
   <select id="<?php echo $this->get_field_id('numberposts'); ?>" name="<?php echo $this->get_field_name('numberposts'); ?>">
   <?php for ($i=1;$i<=20;$i++) {
         echo 
'<option value="'.$i.'"';
         if (
$i==$instance['numberposts']) echo ' selected="selected"';
         echo 
'>'.$i.'</option>';
        } 
?>
       </select>
  </p>

 <!--
  


   <input type="checkbox" id="< ?php echo $this->get_field_id('rss'); ? >" name="< ?php echo $this->get_field_name('rss'); ? >" < ?php if ($instance['rss']) echo 'checked="checked"' ? > />
   <label for="< ?php echo $this->get_field_id('rss'); ? >">Show RSS feed link?</label>
  </p>
     -->
  <?php
}

function 
update ($new_instance$old_instance) {
  
$instance $old_instance;
  
  
$instance['excluded'] = $new_instance['excluded'];
  
$instance['catid'] = $new_instance['catid'];
  
$instance['numberposts'] = $new_instance['numberposts'];
  
$instance['title'] = $new_instance['title'];
  
 
// $instance['rss'] = $new_instance['rss'];

  
return $instance;
}

function 
widget ($args,$instance) {
   
extract($args);
  
  
$excluded $instance['excluded'];
  
$title $instance['title'];
  
$catid $instance['catid'];
  
$numberposts $instance['numberposts'];
  
  
//$rss = $instance['rss'];

  // retrieve posts information from database '.$catid 
  
global $wpdb;
  
$posts get_posts('numberposts='.$numberposts.'&cat=-160,-169');
  
$out '<ul>';
  foreach(
$posts as $post) {
  
$out .= '[*][b][url="'.get_permalink($post->ID).'"]'.$post->post_title.'[/url]</strong';
  }
  
//if ($rss) $out .= '[*][url="'.get_category_link($catid).'feed/"]Category RSS[/url]';
  
$out .= '[/list]';

  
//print the widget for the sidebar
  
echo $before_widget;
  echo 
$before_title.$title.$after_title;
  echo 
$out;
  echo 
$after_widget;
 }
}

function 
ahspfc_load_widgets() {
  
register_widget('Ricky_Recent_Posts');
}

add_action('widgets_init''ahspfc_load_widgets');