wid-postlist.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. add_action( 'widgets_init', 'd_postlists' );
  3. function d_postlists() {
  4. register_widget( 'd_postlist' );
  5. }
  6. class d_postlist extends WP_Widget {
  7. function d_postlist() {
  8. $widget_ops = array( 'classname' => 'd_postlist', 'description' => '图文展示(最新文章+热门文章+随机文章)' );
  9. $this->WP_Widget( 'd_postlist', 'Yusi-聚合文章', $widget_ops, $control_ops );
  10. }
  11. function widget( $args, $instance ) {
  12. extract( $args );
  13. $title = apply_filters('widget_name', $instance['title']);
  14. $limit = $instance['limit'];
  15. $cat = $instance['cat'];
  16. $orderby = $instance['orderby'];
  17. $more = $instance['more'];
  18. $link = $instance['link'];
  19. $img = $instance['img'];
  20. $mo='';
  21. $style='';
  22. if( $more!='' && $link!='' ) $mo='<a class="btn" href="'.$link.'">'.$more.'</a>';
  23. if( !$img ) $style = ' class="nopic"';
  24. echo $before_widget;
  25. echo $before_title.$mo.$title.$after_title;
  26. echo '<ul'.$style.'>';
  27. echo dtheme_posts_list( $orderby,$limit,$cat,$img );
  28. echo '</ul>';
  29. echo $after_widget;
  30. }
  31. function form( $instance ) {
  32. ?>
  33. <p>
  34. <label>
  35. 标题:
  36. <input style="width:100%;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
  37. </label>
  38. </p>
  39. <p>
  40. <label>
  41. 排序:
  42. <select style="width:100%;" id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>" style="width:100%;">
  43. <option value="comment_count" <?php selected('comment_count', $instance['orderby']); ?>>评论数</option>
  44. <option value="date" <?php selected('date', $instance['orderby']); ?>>发布时间</option>
  45. <option value="rand" <?php selected('rand', $instance['orderby']); ?>>随机</option>
  46. </select>
  47. </label>
  48. </p>
  49. <p>
  50. <label>
  51. 分类限制:
  52. <a style="font-weight:bold;color:#f60;text-decoration:none;" href="javascript:;" title="格式:1,2 &nbsp;表限制ID为1,2分类的文章&#13;格式:-1,-2 &nbsp;表排除分类ID为1,2的文章&#13;也可直接写1或者-1;注意逗号须是英文的">?</a>
  53. <input style="width:100%;" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $instance['cat']; ?>" size="24" />
  54. </label>
  55. </p>
  56. <p>
  57. <label>
  58. 显示数目:
  59. <input style="width:100%;" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="number" value="<?php echo $instance['limit']; ?>" size="24" />
  60. </label>
  61. </p>
  62. <p>
  63. <label>
  64. More 显示文字:
  65. <input style="width:100%;" id="<?php echo $this->get_field_id('more'); ?>" name="<?php echo $this->get_field_name('more'); ?>" type="text" value="<?php echo $instance['more']; ?>" size="24" />
  66. </label>
  67. </p>
  68. <p>
  69. <label>
  70. More 链接:
  71. <input style="width:100%;" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="url" value="<?php echo $instance['link']; ?>" size="24" />
  72. </label>
  73. </p>
  74. <p>
  75. <label>
  76. <input style="vertical-align:-3px;margin-right:4px;" class="checkbox" type="checkbox" <?php checked( $instance['img'], 'on' ); ?> id="<?php echo $this->get_field_id('img'); ?>" name="<?php echo $this->get_field_name('img'); ?>">显示图片
  77. </label>
  78. </p>
  79. <?php
  80. }
  81. }
  82. function dtheme_posts_list($orderby,$limit,$cat,$img) {
  83. $args = array(
  84. 'order' => DESC,
  85. 'cat' => $cat,
  86. 'orderby' => $orderby,
  87. 'showposts' => $limit,
  88. 'caller_get_posts' => 1
  89. );
  90. query_posts($args);
  91. while (have_posts()) : the_post();
  92. ?>
  93. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" ><?php if( $img ){echo '<span class="thumbnail">';echo '<img src="'.get_bloginfo("template_url").'/timthumb.php?src=';echo post_thumbnail_src();echo '&h=64&w=100&q=90&zc=1&ct=1" alt="'.get_the_title().'" /></span>'; }else{$img = '';} ?><span class="text"><?php the_title(); ?></span><span class="muted"><?php the_time('Y-m-d');?></span><span class="muted"><?php comments_number('', '1评论', '%评论'); ?></span></a></li>
  94. <?php
  95. endwhile; wp_reset_query();
  96. }
  97. ?>