我正在使用一个名为“最近分类帖子小部件”的插件。不幸的是,插件开发人员不再提供支持。这个插件的编码方式只显示小部件的一个实例(这意味着我只能使用这个小部件一次)。为了使用小部件的多个实例,以下代码中必须更改哪些内容?
<?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;
}
}