Favorite recent post widget

时间:2011-04-19 作者:Gregg Franklin

是否有人有最喜欢的边栏小部件,可以显示最后三篇文章:

标题部分内容。。。阅读更多信息

2 个回复
SO网友:Grimosos

您可以创建一个小部件(Create a Widget) 像这样:

<?php /*
Plugin Name: WDG
Plugin URI: 
Description: Plugin
Version: 0.1
Author URI: 
*/
add_action( \'widgets_init\', \'my_wdg\' );
function my_wdg() {
    register_widget( \'Wdg\' );
}
class Wdg extends WP_Widget {
    function Wdg() {
            /* Settings of widget */
        $widget_ops = array( \'classname\' => \'Widget\', \'description\' => \' Plugin\' );

        /* Control settings of widget */
        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'wdg\' );

        /* Create widget */
        $this->WP_Widget( \'wdg\', \'Wdg\', $widget_ops, $control_ops );
}
    function widget( $args, $instance ) {
    extract( $args );

    /* User-selected settings. */
    $title = apply_filters(\'widget_title\', $instance[\'title\'] );

    /* Before widget (theme defined). */
    echo $before_widget;

    /* Widget title (before e after theme defined). */
    if ( $title )
        echo $before_title . $title . $after_title;
             echo\'     <div id="recent">
            <ul>\';
             rewind_posts(); 
             query_posts(\'posts_per_page=3\');
                     if ( have_posts() ) : while ( have_posts() ) : the_post();?>
                        <li>
                         <h5><a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                          <?php the_title();?>
                         </a></h5>
                         <span>
                          <?php the_excerpt ?>
                         </span>
                        </li>
                      <?php endwhile; else: ?>
                      <p>I\'m sorry</p>
                    <?php endif; ?>
                   </ul>
                 </div>
           <?php echo $after_widget; }
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    /* Strip tags modify qidget configuration. */
      $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
    return $instance;
}

function form( $instance ) {

    /* default widget setting */
    $defaults = array(              
                    \'title\' =>  \'Recent Comments\',
        );
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
    <label for="<?php echo $this->get_field_id( \'title\' ); ?>">Title:</label>
        <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
</p>
<?php
    } 
 }  ?>
这个小部件可能会工作!

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?