如何显示一个小部件的多个实例?

时间:2011-10-20 作者:Sledge81

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

<?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;
    }

}

1 个回复
最合适的回答,由SO网友:Alex Sancho 整理而成

您需要重新编码插件以使用Wordpress Widget API

结束

相关推荐

Widgets in PHP files?

有没有可能让Wordpress上的每个小部件都以不同的方式运行。php文件我有一个包含12个元素的页面,我想让每个元素都成为一个小部件,以便以后更容易管理/编辑它们,但如果它来自php页面,而不是来自管理面板上的代码块,则会更好。