mh_custom_posts_widget

The MH Corporate basic 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-corporate-basic'),  
  5. array( 
  6. 'classname' => 'mh_custom_posts',  
  7. 'description' => esc_html__('Custom Posts Widget to display posts based on categories.', 'mh-corporate-basic'),  
  8. 'customize_selective_refresh' => true 
  9. ); 
  10. function widget($args, $instance) { 
  11. extract($args); 
  12. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
  13. $category = isset($instance['category']) ? $instance['category'] : ''; 
  14. $postcount = empty($instance['postcount']) ? '5' : $instance['postcount']; 
  15. $offset = empty($instance['offset']) ? '' : $instance['offset']; 
  16. $sticky = isset($instance['sticky']) ? $instance['sticky'] : 1; 
  17.  
  18. echo $before_widget; 
  19. if (!empty($title)) { echo $before_title . $title . $after_title; } ?> 
  20. <ul class="cp-widget clearfix"> <?php 
  21. $args = array('posts_per_page' => $postcount, 'offset' => $offset, 'cat' => $category, 'ignore_sticky_posts' => $sticky); 
  22. $counter = 1; 
  23. $widget_loop = new WP_Query($args); 
  24. while ($widget_loop->have_posts()) : $widget_loop->the_post(); 
  25. if ($counter == 1) : ?> 
  26. <li class="cp-wrap clearfix"> 
  27. <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> 
  28. <div class="cp-data"> 
  29. <h3 class="cp-xl-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> 
  30. <p class="meta"> 
  31. <?php $post_date = get_the_date(); ?> 
  32. <?php echo $post_date . ' | '; ?> 
  33. <?php mh_comment_count(); ?> 
  34. </p> 
  35. </div> 
  36. <?php mh_excerpt(); ?> 
  37. </li><?php 
  38. else : ?> 
  39. <li class="cp-wrap cp-small clearfix"> 
  40. <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> 
  41. <div class="cp-data"> 
  42. <p class="cp-widget-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
  43. <p class="meta"><?php comments_number(__('0 Comments', 'mh-corporate-basic'), __('1 Comment', 'mh-corporate-basic'), __('% Comments', 'mh-corporate-basic')); ?></p> 
  44. </div> 
  45. </li><?php 
  46. endif; 
  47. $counter++; 
  48. endwhile; 
  49. wp_reset_postdata(); ?> 
  50. </ul><?php 
  51. echo $after_widget; 
  52. function update($new_instance, $old_instance) { 
  53. $instance = $old_instance; 
  54. $instance['title'] = sanitize_text_field($new_instance['title']); 
  55. $instance['category'] = absint($new_instance['category']); 
  56. $instance['postcount'] = absint($new_instance['postcount']); 
  57. $instance['offset'] = absint($new_instance['offset']); 
  58. $instance['sticky'] = isset($new_instance['sticky']) ? strip_tags($new_instance['sticky']) : ''; 
  59. return $instance; 
  60. function form($instance) { 
  61. $defaults = array('title' => '', 'category' => '', 'postcount' => '5', 'offset' => '0', 'sticky' => 1); 
  62. $instance = wp_parse_args((array) $instance, $defaults); ?> 
  63.  
  64. <p> 
  65. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mh-corporate-basic'); ?></label> 
  66. <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'); ?>" /> 
  67. </p> 
  68. <p> 
  69. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Select a Category:', 'mh-corporate-basic'); ?></label> 
  70. <select id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>"> 
  71. <option value="0" <?php if (!$instance['category']) echo 'selected="selected"'; ?>><?php _e('All', 'mh-corporate-basic'); ?></option> 
  72. <?php 
  73. $categories = get_categories(array('type' => 'post')); 
  74. foreach($categories as $cat) { 
  75. echo '<option value="' . $cat->cat_ID . '"'; 
  76. if ($cat->cat_ID == $instance['category']) { echo ' selected="selected"'; } 
  77. echo '>' . $cat->cat_name . ' (' . $cat->category_count . ')'; 
  78. echo '</option>'; 
  79. ?> 
  80. </select> 
  81. </p> 
  82. <p> 
  83. <label for="<?php echo $this->get_field_id('postcount'); ?>"><?php _e('Limit Post Number:', 'mh-corporate-basic'); ?></label> 
  84. <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'); ?>" /> 
  85. </p> 
  86. <p> 
  87. <label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Skip Posts (Offset):', 'mh-corporate-basic'); ?></label> 
  88. <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'); ?>" /> 
  89. </p> 
  90. <p> 
  91. <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']); ?>/> 
  92. <label for="<?php echo $this->get_field_id('sticky'); ?>"><?php _e('Ignore Sticky Posts', 'mh-corporate-basic'); ?></label> 
  93. </p> 
  94. <p> 
  95. <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-corporate-basic'); ?> 
  96. </p><?php