Advertisement
Guest User

One Wiki Widget

a guest
Jun 30th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.24 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('widgets_init', 'pyre_one_wiki_load_widgets');
  4.  
  5. function pyre_one_wiki_load_widgets()
  6. {
  7.     register_widget('Pyre_One_Wiki_Widget');
  8. }
  9.  
  10. class Pyre_One_Wiki_Widget extends WP_Widget {
  11.  
  12.     function Pyre_One_Wiki_Widget()
  13.     {
  14.         $widget_ops = array('classname' => 'pyre_one_wiki', 'description' => 'Recent OneWiki posts widget.');
  15.         $control_ops = array('id_base' => 'pyre_one_wiki-widget');
  16.  
  17.         $this->WP_Widget('pyre_one_wiki-widget', 'My Theme: One Wiki', $widget_ops, $control_ops);
  18.     }
  19.  
  20.     function widget($args, $instance)
  21.     {
  22.         extract($args);
  23.         $title = $instance['title'];
  24.         $post_type = 'all';
  25.         $categories = $instance['categories'];
  26.         $posts = $instance['posts'];
  27.         $images = true;
  28.         $rating = true;
  29.         $show_excerpt = isset($instance['show_excerpt']) ? 'true' : 'false';
  30.  
  31.         echo $before_widget;
  32. ?>
  33.  
  34. <?php
  35.     $post_types = get_post_types();
  36.     unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
  37.     if($post_type == 'all') {
  38.         $post_type_array = $post_types;
  39.         }
  40.     else {
  41.         $post_type_array = $post_type;
  42.         }
  43. ?>
  44.  
  45.     <div class="block full">
  46.     <h3><a href="<?php echo get_category_link($categories); ?>"><?php echo $title; ?></a> <span class="arrows">&raquo;</span></h3>
  47.     <?php
  48.         $recent_posts = new WP_Query(array(
  49.             'showposts' => $posts,
  50.             'cat' => $categories,
  51.             ));
  52.     ?>
  53.     <?php
  54.         $big_count = round($posts / 9);
  55.         if(!$big_count) { $big_count = 1; }
  56.     ?>
  57.     <?php $counter = 1; while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
  58.     <?php
  59.         if(has_post_format('video') || has_post_format('audio') || has_post_format('gallery')) {
  60.             $icon = '<span class="' . get_post_format($post->ID) . '-icon"></span>';
  61.             }
  62.         else {
  63.             $icon = '';
  64.             }
  65.     ?>
  66.     <?php if($counter <= $big_count): ?>
  67.     <?php if($counter == $big_count) { $last = 'block-item-big-last'; } else { $last = ''; }?>
  68.     <div class="block-item-big <?php echo $last; ?>">
  69.     <?php if($images && has_post_thumbnail()): ?>
  70.     <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image'); ?>
  71.     <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
  72.     <?php else: ?>
  73.     <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=290&h=160" alt="<?php the_title(); ?>" width='290' height='160' /></a><?php echo $icon; ?></div>
  74.     <?php endif; ?>
  75.     <h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2><h4><?php echo ' - ' ?><?php the_time('F j, Y'); ?></h4>
  76.     <?php if($show_excerpt == 'true'): ?><p><?php echo string_limit_words(get_the_excerpt(), 15); ?><a href='<?php the_permalink(); ?>'> [...]</a></p><?php endif; ?>
  77.     </div>
  78.     <?php else: ?>
  79.     <div class="block-item-small">
  80.     <!--<?php if($images && has_post_thumbnail()): ?>
  81.     <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image-thumb'); ?>
  82.     <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" width='50' height='50' /></a><?php echo $icon; ?></div>
  83.     <?php else: ?>
  84.     <div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=50&h=50" alt="<?php the_title(); ?>"  width='50' height='50' /></a><?php echo $icon; ?></div>
  85.     <?php endif; ?>-->
  86.     <h4><?php echo the_time('d/n'); ?><?php echo ' - ' ?><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h4>
  87.     </div>
  88.     <?php endif; ?>
  89.     <?php $counter++; endwhile; ?>
  90.     </div>
  91.  
  92.     <?php
  93.  
  94.         echo $after_widget;
  95.     }
  96.  
  97.     function update($new_instance, $old_instance)
  98.     {
  99.         $instance = $old_instance;
  100.         $instance['title'] = $new_instance['title'];
  101.         $instance['post_type'] = 'all';
  102.         $instance['categories'] = $new_instance['categories'];
  103.         $instance['posts'] = $new_instance['posts'];
  104.         $instance['show_images'] = true;
  105.         $instance['show_rating'] = true;
  106.         $instance['show_excerpt'] = $new_instance['show_excerpt'];
  107.  
  108.         return $instance;
  109.     }
  110.  
  111.     function form($instance)
  112.     {
  113.         $defaults = array('title' => 'Recent Posts', 'post_type' => 'all', 'categories' => 'all', 'posts' => 4, 'show_excerpt' => null);
  114.         $instance = wp_parse_args((array) $instance, $defaults); ?>
  115.         <p>
  116.             <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
  117.             <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
  118.         </p>
  119.         <p>
  120.             <label for="<?php echo $this->get_field_id('categories'); ?>">Filter by Category:</label>
  121.             <select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
  122.                 <option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>all categories</option>
  123.                 <?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
  124.                 <?php foreach($categories as $category) { ?>
  125.                 <option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
  126.                 <?php } ?>
  127.             </select>
  128.         </p>
  129.         <p>
  130.             <label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts:</label>
  131.             <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
  132.         </p>
  133.         <p>
  134.             <input class="checkbox" type="checkbox" <?php checked($instance['show_excerpt'], 'on'); ?> id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>" />
  135.             <label for="<?php echo $this->get_field_id('show_excerpt'); ?>">Show excerpt</label>
  136.         </p>
  137. <?php
  138.     }
  139. }
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement