J'utilise un plugin appelé widget de publication de catégorie récente. Malheureusement, le développeur du plugin n'offre plus d'assistance. La manière dont ce plugin est codé n'affiche qu'une seule instance du widget (ce qui signifie que je ne peux utiliser ce widget qu'une seule fois). Que doit-on changer dans le code ci-dessous pour pouvoir utiliser plusieurs instances du widget?
<?php
register_activation_hook ( __FILE__, array('single_category_posts_widget', 'activate') );
register_deactivation_hook ( __FILE__, array('single_category_posts_widget', 'deactivate') );
add_action ( "widgets_init", array('single_category_posts_widget', 'register') );
class single_category_posts_widget {
function activate()
{
if( get_option( 'single_category_posts_widget_title' ) === FALSE ) {
update_option( 'single_category_posts_widget_title', 'Recent Category Posts' );
}
if( get_option( 'single_category_posts_widget_category' ) === FALSE ) {
update_option( 'single_category_posts_widget_category', 0 );
}
if( get_option( 'single_category_posts_widget_qty' ) === FALSE ) {
update_option( 'single_category_posts_widget_qty', 5 );
}
}
function deactivate()
{
delete_option( 'single_category_posts_widget_title' );
delete_option( 'single_category_posts_widget_category' );
delete_option( 'single_category_posts_widget_qty' );
}
function register()
{
wp_register_sidebar_widget( 'recent-category-posts', 'Recent Posts in Category', array('single_category_posts_widget', 'widget' ));
wp_register_widget_control( 'recent-category-posts', 'Recent Posts in Category', array('single_category_posts_widget', 'control' ));
}
function control()
{
if (isset($_POST['single_category_posts_widget_title'])) update_option( 'single_category_posts_widget_title', mysql_real_escape_string($_POST['single_category_posts_widget_title']) );
if (isset($_POST['single_category_posts_widget_category'])) update_option( 'single_category_posts_widget_category', intval($_POST['single_category_posts_widget_category']) );
if (isset($_POST['single_category_posts_widget_qty'])) update_option( 'single_category_posts_widget_qty', intval($_POST['single_category_posts_widget_qty']) );
?>
<p><label>
<strong>Widget Title:</strong><br />
<input class="widefat" type="text" name="single_category_posts_widget_title" value="<?php echo get_option( 'single_category_posts_widget_title' ); ?>" /></label></p>
<p><label>
<strong>What Category:</strong><br />
<?php wp_dropdown_categories( Array(
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 0,
'hide_if_empty' => false,
'echo' => 1,
'selected' => get_option( 'single_category_posts_widget_category' ),
'hierarchical' => 1,
'name' => 'single_category_posts_widget_category',
'id' => 'single_category_posts_widget_category',
'class' => 'widefat',
'taxonomy' => 'category',
) ); ?></label></p>
<p><label>
<strong>How Many Posts:</strong><br />
<input class="widefat" type="text" name="single_category_posts_widget_qty" value="<?php echo get_option( 'single_category_posts_widget_qty' ); ?>" /></label></p>
<?php
}
function widget( $args )
{
echo $args['before_widget'];
echo $args['before_title'] . get_option( 'single_category_posts_widget_title', 'Recent Category Posts' ) . $args['after_title'];
echo self::get_cat_posts( get_option( 'single_category_posts_widget_category', 0 ), get_option( 'single_category_posts_widget_qty', 5 ) );
echo $args['after_widget'];
}
function get_cat_posts( $cat, $qty )
{
$posts = get_posts( Array(
'cat' => $cat,
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => $qty
));
$returnThis = '';
if( count( $posts ) )
$returnThis .= '<ul>'."\r\n";
foreach( $posts as $post )
$returnThis .= "\t".'<li><a href="'.get_permalink( $post->ID ).'">'.$post->post_title.'</a></li>'."\r\n";
if( count( $posts ) )
$returnThis .= '</ul>'."\r\n";
return $returnThis;
}
}
Vous devez recoder votre plugin pour utiliser l’API Wordpress Widget