mh_newsdesk_lite_custom_posts

The MH Newsdesk lite mh newsdesk lite custom posts class.

Defined (1)

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

/includes/widgets/mh-custom-posts.php  
  1. class mh_newsdesk_lite_custom_posts extends WP_Widget { 
  2. function __construct() { 
  3. parent::__construct( 
  4. 'mh_newsdesk_lite_custom_posts', esc_html_x('MH Custom Posts [lite]', 'widget name', 'mh-newsdesk-lite'),  
  5. array( 
  6. 'classname' => 'mh_newsdesk_lite_custom_posts',  
  7. 'description' => esc_html__('Display posts from any category.', 'mh-newsdesk-lite'),  
  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. if ($category) { 
  19. $cat_url = get_category_link($category); 
  20. $before_title = $before_title . '<a href="' . esc_url($cat_url) . '" class="widget-title-link">'; 
  21. $after_title = '</a>' . $after_title; 
  22.  
  23. echo $before_widget; 
  24. if (!empty($title)) { echo $before_title . esc_attr($title) . $after_title; } 
  25. $args = array('posts_per_page' => $postcount, 'offset' => $offset, 'cat' => $category, 'ignore_sticky_posts' => $sticky); 
  26. $widget_loop = new WP_Query($args); ?> 
  27. <div class="mh-cp-widget clearfix"><?php 
  28. while ($widget_loop->have_posts()) : $widget_loop->the_post(); ?> 
  29. <article class="cp-wrap cp-small clearfix"> 
  30. <div class="cp-thumb-small"> 
  31. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php if (has_post_thumbnail()) { the_post_thumbnail('cp-thumb-small'); } else { echo '<img src="' . get_template_directory_uri() . '/images/placeholder-thumb-small.jpg' . '" alt="No Picture" />'; } ?></a> 
  32. </div> 
  33. <p class="entry-meta"><span class="updated"><?php echo get_the_date(); ?></span></p> 
  34. <h3 class="cp-title-small"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h3> 
  35. </article> 
  36. <hr class="mh-separator"><?php 
  37. endwhile; 
  38. wp_reset_postdata(); ?> 
  39. </div><?php 
  40. echo $after_widget; 
  41. function update($new_instance, $old_instance) { 
  42. $instance = $old_instance; 
  43. $instance['title'] = sanitize_text_field($new_instance['title']); 
  44. $instance['category'] = absint($new_instance['category']); 
  45. $instance['postcount'] = absint($new_instance['postcount']); 
  46. $instance['offset'] = absint($new_instance['offset']); 
  47. $instance['sticky'] = isset($new_instance['sticky']) ? strip_tags($new_instance['sticky']) : ''; 
  48. return $instance; 
  49. function form($instance) { 
  50. $defaults = array('title' => '', 'category' => '', 'postcount' => '5', 'offset' => '0', 'sticky' => 1); 
  51. $instance = wp_parse_args((array) $instance, $defaults); ?> 
  52.  
  53. <p> 
  54. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mh-newsdesk-lite'); ?></label> 
  55. <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'); ?>" /> 
  56. </p> 
  57. <p> 
  58. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Select a Category:', 'mh-newsdesk-lite'); ?></label> 
  59. <select id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>"> 
  60. <option value="0" <?php if (!$instance['category']) echo 'selected="selected"'; ?>><?php _e('All', 'mh-newsdesk-lite'); ?></option> 
  61. <?php 
  62. $categories = get_categories(array('type' => 'post')); 
  63. foreach($categories as $cat) { 
  64. echo '<option value="' . $cat->cat_ID . '"'; 
  65. if ($cat->cat_ID == $instance['category']) { echo ' selected="selected"'; } 
  66. echo '>' . $cat->cat_name . ' (' . $cat->category_count . ')'; 
  67. echo '</option>'; 
  68. ?> 
  69. </select> 
  70. </p> 
  71. <p> 
  72. <label for="<?php echo $this->get_field_id('postcount'); ?>"><?php _e('Limit Post Number:', 'mh-newsdesk-lite'); ?></label> 
  73. <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'); ?>" /> 
  74. </p> 
  75. <p> 
  76. <label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Skip Posts (Offset):', 'mh-newsdesk-lite'); ?></label> 
  77. <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'); ?>" /> 
  78. </p> 
  79. <p> 
  80. <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']); ?>/> 
  81. <label for="<?php echo $this->get_field_id('sticky'); ?>"><?php _e('Ignore Sticky Posts', 'mh-newsdesk-lite'); ?></label> 
  82. </p> 
  83. <p> 
  84. <strong><?php _e('Info:', 'mh-newsdesk-lite'); ?></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-newsdesk-lite'); ?> 
  85. </p><?php