mh_custom_posts_widget

The MH Corporate lite mh custom posts widget class.

Defined (1)

The class is defined in the following location(s).

/includes/widgets/mh-custom-posts.php  
  1. class mh_custom_posts_widget extends WP_Widget { 
  2. function mh_custom_posts_widget() { 
  3. $widget_ops = array('classname' => 'mh_custom_posts', 'description' => __('Custom Posts Widget to display posts based on categories.', 'mhc')); 
  4. $this->WP_Widget('mh_custom_posts', __('MH Custom Posts lite', 'mhc'), $widget_ops); 
  5. function widget($args, $instance) { 
  6. extract($args); 
  7. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
  8. $category = isset($instance['category']) ? $instance['category'] : ''; 
  9. $postcount = empty($instance['postcount']) ? '5' : $instance['postcount']; 
  10. $offset = empty($instance['offset']) ? '' : $instance['offset']; 
  11. $sticky = isset($instance['sticky']) ? $instance['sticky'] : 0; 
  12.  
  13. echo $before_widget; 
  14. if (!empty($title)) { echo $before_title . $title . $after_title; } ?> 
  15. <ul class="cp-widget clearfix"> <?php 
  16. $args = array('posts_per_page' => $postcount, 'offset' => $offset, 'cat' => $category, 'ignore_sticky_posts' => $sticky); 
  17. $counter = 1; 
  18. $widget_loop = new WP_Query($args); 
  19. while ($widget_loop->have_posts()) : $widget_loop->the_post(); 
  20. if ($counter == 1) : ?> 
  21. <li class="cp-wrap clearfix"> 
  22. <div class="cp-thumb-xl"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail('cp_large'); } else { echo '<img src="' . get_template_directory_uri() . '/images/noimage_cp_large.png' . '" alt="No Picture" />'; } ?></a></div> 
  23. <div class="cp-data"> 
  24. <h3 class="cp-xl-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> 
  25. <p class="meta"> 
  26. <?php $post_date = get_the_date(); ?> 
  27. <?php echo $post_date . ' | '; ?> 
  28. <?php mh_comment_count(); ?> 
  29. </p> 
  30. </div> 
  31. <?php mh_excerpt(); ?> 
  32. </li><?php 
  33. else : ?> 
  34. <li class="cp-wrap cp-small clearfix"> 
  35. <div class="cp-thumb"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail('cp_small'); } else { echo '<img src="' . get_template_directory_uri() . '/images/noimage_70x53.png' . '" alt="No Picture" />'; } ?></a></div> 
  36. <div class="cp-data"> 
  37. <p class="cp-widget-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
  38. <p class="meta"><?php comments_number(__('0 Comments', 'mhc'), __('1 Comment', 'mhc'), __('% Comments', 'mhc')); ?></p> 
  39. </div> 
  40. </li><?php 
  41. endif; 
  42. $counter++; 
  43. endwhile; 
  44. wp_reset_postdata(); ?> 
  45. </ul><?php 
  46. echo $after_widget; 
  47. function update($new_instance, $old_instance) { 
  48. $instance = $old_instance; 
  49. $instance['title'] = strip_tags($new_instance['title']); 
  50. $instance['category'] = strip_tags($new_instance['category']); 
  51. $instance['postcount'] = strip_tags($new_instance['postcount']); 
  52. $instance['offset'] = strip_tags($new_instance['offset']); 
  53. $instance['sticky'] = strip_tags($new_instance['sticky']); 
  54. return $instance; 
  55. function form($instance) { 
  56. $defaults = array('title' => '', 'category' => '', 'postcount' => '5', 'offset' => '0', 'sticky' => 0); 
  57. $instance = wp_parse_args((array) $instance, $defaults); ?> 
  58.  
  59. <p> 
  60. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mhc'); ?></label> 
  61. <input class="widefat" type="text" value="<?php echo esc_attr($instance['title']); ?>" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" /> 
  62. </p> 
  63. <p> 
  64. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Select a Category:', 'mhc'); ?></label> 
  65. <select id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>"> 
  66. <option value="0" <?php if (!$instance['category']) echo 'selected="selected"'; ?>><?php _e('All', 'mhc'); ?></option> 
  67. <?php 
  68. $categories = get_categories(array('type' => 'post')); 
  69. foreach($categories as $cat) { 
  70. echo '<option value="' . $cat->cat_ID . '"'; 
  71. if ($cat->cat_ID == $instance['category']) { echo ' selected="selected"'; } 
  72. echo '>' . $cat->cat_name . ' (' . $cat->category_count . ')'; 
  73. echo '</option>'; 
  74. ?> 
  75. </select> 
  76. </p> 
  77. <p> 
  78. <label for="<?php echo $this->get_field_id('postcount'); ?>"><?php _e('Limit Post Number:', 'mhc'); ?></label> 
  79. <input class="widefat" type="text" value="<?php echo esc_attr($instance['postcount']); ?>" name="<?php echo $this->get_field_name('postcount'); ?>" id="<?php echo $this->get_field_id('postcount'); ?>" /> 
  80. </p> 
  81. <p> 
  82. <label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Skip Posts (Offset):', 'mhc'); ?></label> 
  83. <input class="widefat" type="text" value="<?php echo esc_attr($instance['offset']); ?>" name="<?php echo $this->get_field_name('offset'); ?>" id="<?php echo $this->get_field_id('offset'); ?>" /> 
  84. </p> 
  85. <p> 
  86. <input id="<?php echo $this->get_field_id('sticky'); ?>" name="<?php echo $this->get_field_name('sticky'); ?>" type="checkbox" value="1" <?php checked('1', $instance['sticky']); ?>/> 
  87. <label for="<?php echo $this->get_field_id('sticky'); ?>"><?php _e('Ignore Sticky Posts', 'mhc'); ?></label> 
  88. </p> 
  89. <p> 
  90. <strong>Info:</strong> <?php _e('This is the lite version of this widget with basic features. If you need more professional features and options, you can upgrade to the premium version of this theme.', 'mhp'); ?> 
  91. </p><?php