__( 'Custom recent posts with image and by categories', 'hgwmag' ),// description
)
);
add_action( 'widgets_init', function() {
register_widget( 'HgwMag_Recent_Posts_Widget' );
});
}
public $args = array(
'before_title' => '
',
'before_widget' => '',
'after_widget' => '
'
);
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
// Class
$BTClass = $args['before_title'];
$ATClass = $args['after_title'];
$BWClass = $args['before_widget'];
$AWClass = $args['after_widget'];
// Widget Fields
$WTitle = ( ! empty( $instance['hgw_recent_post_title'] ) ) ? $instance['hgw_recent_post_title'] : '';
$WPostsNumber = ( ! empty( $instance['hgw_recent_post_postsnum'] ) ) ? $instance['hgw_recent_post_postsnum'] : '5';
$WCategory = ( !empty( $instance['hgw_recent_post_caregory'] ) ) ? $instance['hgw_recent_post_caregory'] : '';
$WDate = isset( $instance['hgw_recent_post_date'] ) ? $instance['hgw_recent_post_date'] : false;
$WComments = isset( $instance['hgw_recent_post_comments'] ) ? $instance['hgw_recent_post_comments'] : false;
// Start
echo $BWClass; // Before Widget Class
if ($WTitle) {
echo $BTClass . $WTitle . $ATClass;
}
$postloop = new WP_Query(
array(
'posts_per_page' => $WPostsNumber,
'cat' => $WCategory
)
);
if ( $postloop->have_posts() ) {
echo '";
}
else {
echo '';
}
wp_reset_postdata();
echo $AWClass; // After Widget Class
// END
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['hgw_recent_post_title'] = sanitize_text_field( $new_instance['hgw_recent_post_title'] );
$instance['hgw_recent_post_caregory'] = ( !empty( $new_instance['hgw_recent_post_caregory'] ) ) ? $new_instance['hgw_recent_post_caregory'] : '';
$instance['hgw_recent_post_postsnum'] = (int) $new_instance['hgw_recent_post_postsnum'];
$instance['hgw_recent_post_date'] = isset( $new_instance['hgw_recent_post_date'] ) ? (bool) $new_instance['hgw_recent_post_date'] : false;
$instance['hgw_recent_post_comments'] = isset( $new_instance['hgw_recent_post_comments'] ) ? (bool) $new_instance['hgw_recent_post_comments'] : false;
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// Title Box
$title = isset( $instance['hgw_recent_post_title'] ) ? esc_attr( $instance['hgw_recent_post_title'] ) : '';
?>
id="get_field_id( 'hgw_recent_post_date' ); ?>" name="get_field_name( 'hgw_recent_post_date' ); ?>" />
id="get_field_id( 'hgw_recent_post_comments' ); ?>" name="get_field_name( 'hgw_recent_post_comments' ); ?>" />