mh_custom_posts_widget

The MH Purity 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 __construct() { 
  3. parent::__construct( 
  4. 'mh_custom_posts', esc_html_x('MH Custom Posts [lite]', 'widget name', 'mh-purity-lite'),  
  5. array( 
  6. 'classname' => 'mh_custom_posts',  
  7. 'description' => esc_html__('Custom Posts Widget to display posts based on categories.', 'mh-purity-lite'),  
  8. 'customize_selective_refresh' => true 
  9. ); 
  10. function widget($args, $instance) { 
  11. extract($args); 
  12. global $post; 
  13. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
  14. $category = isset($instance['category']) ? $instance['category'] : ''; 
  15. $postcount = empty($instance['postcount']) ? '5' : $instance['postcount']; 
  16. $offset = empty($instance['offset']) ? '' : $instance['offset']; 
  17. $excerpt_length = empty($instance['excerpt_length']) ? '110' : $instance['excerpt_length']; 
  18. $sticky = isset($instance['sticky']) ? $instance['sticky'] : 0; 
  19.  
  20. if ($category) { 
  21. $cat_url = get_category_link($category); 
  22. $before_title = $before_title . '<a href="' . esc_url($cat_url) . '" class="widget-title-link">'; 
  23. $after_title = '</a>' . $after_title; 
  24.  
  25. echo $before_widget; 
  26. if (!empty($title)) { echo $before_title . $title . $after_title; } ?> 
  27. <div class="cp-widget clearfix"> <?php 
  28. $args = array('posts_per_page' => $postcount, 'offset' => $offset, 'cat' => $category, 'ignore_sticky_posts' => $sticky); 
  29. $counter = 1; 
  30. $widget_loop = new WP_Query($args); 
  31. while ($widget_loop->have_posts()) : $widget_loop->the_post(); 
  32. if ($counter == 1) : ?> 
  33. <div class="cp-widget-item cp-large cp-count-<?php echo esc_attr($postcount); ?>"> 
  34. <div class="cp-large-thumb"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail('featured'); } else { echo '<img src="' . get_template_directory_uri() . '/images/noimage_featured.png' . '" alt="No Picture" />'; } ?></a></div> 
  35. <div class="meta clearfix"> 
  36. <span class="meta-date"><?php $post_date = get_the_date(); echo $post_date; ?></span> 
  37. <span class="meta-comments"><i class="fa fa-comment-o"></i><?php comments_number('0', '1', '%'); ?></span> 
  38. </div> 
  39. <h3 class="cp-large-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> 
  40. <?php mh_excerpt($excerpt_length); ?> 
  41. </div> 
  42. <ul class="cp-list"><?php 
  43. else: ?> 
  44. <li class="cp-widget-item cp-list-item clearfix"> 
  45. <div class="cp-small-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_cp_small.png' . '" alt="No Picture" />'; } ?></a></div> 
  46. <div class="meta"><?php $post_date = get_the_date(); echo $post_date; ?></div> 
  47. <p class="cp-small-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
  48. </li> <?php 
  49. endif; 
  50. $counter++; 
  51. endwhile; 
  52. wp_reset_postdata(); ?> 
  53. </ul> 
  54. </div><?php 
  55. echo $after_widget; 
  56. function update($new_instance, $old_instance) { 
  57. $instance = $old_instance; 
  58. $instance['title'] = sanitize_text_field($new_instance['title']); 
  59. $instance['category'] = absint($new_instance['category']); 
  60. $instance['postcount'] = absint($new_instance['postcount']); 
  61. $instance['offset'] = absint($new_instance['offset']); 
  62. $instance['excerpt_length'] = absint($new_instance['excerpt_length']); 
  63. $instance['sticky'] = isset($new_instance['sticky']) ? strip_tags($new_instance['sticky']) : ''; 
  64. return $instance; 
  65. function form($instance) { 
  66. $defaults = array('title' => '', 'category' => '', 'postcount' => '5', 'offset' => '0', 'excerpt_length' => '110', 'sticky' => 0); 
  67. $instance = wp_parse_args((array) $instance, $defaults); ?> 
  68.  
  69. <p> 
  70. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mh-purity-lite'); ?></label> 
  71. <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'); ?>" /> 
  72. </p> 
  73. <p> 
  74. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Select a Category:', 'mh-purity-lite'); ?></label> 
  75. <select id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>"> 
  76. <option value="0" <?php if (!$instance['category']) echo 'selected="selected"'; ?>><?php _e('All', 'mh-purity-lite'); ?></option> 
  77. <?php 
  78. $categories = get_categories(array('type' => 'post')); 
  79. foreach($categories as $cat) { 
  80. echo '<option value="' . $cat->cat_ID . '"'; 
  81. if ($cat->cat_ID == $instance['category']) { echo ' selected="selected"'; } 
  82. echo '>' . $cat->cat_name . ' (' . $cat->category_count . ')'; 
  83. echo '</option>'; 
  84. ?> 
  85. </select> 
  86. </p> 
  87. <p> 
  88. <label for="<?php echo $this->get_field_id('postcount'); ?>"><?php _e('Limit Post Number:', 'mh-purity-lite'); ?></label> 
  89. <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'); ?>" /> 
  90. </p> 
  91. <p> 
  92. <label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Skip Posts (Offset):', 'mh-purity-lite'); ?></label> 
  93. <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'); ?>" /> 
  94. </p> 
  95. <p> 
  96. <label for="<?php echo $this->get_field_id('excerpt_length'); ?>"><?php _e('Excerpt Character Limit:', 'mh-purity-lite'); ?></label> 
  97. <input class="widefat" type="text" value="<?php echo esc_attr($instance['excerpt_length']); ?>" name="<?php echo $this->get_field_name('excerpt_length'); ?>" id="<?php echo $this->get_field_id('excerpt_length'); ?>" /> 
  98. </p> 
  99. <p> 
  100. <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']); ?>/> 
  101. <label for="<?php echo $this->get_field_id('sticky'); ?>"><?php _e('Ignore Sticky Posts', 'mh-purity-lite'); ?></label> 
  102. </p> 
  103. <p> 
  104. <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.', 'mh-purity-lite'); ?> 
  105. </p><?php