Skip to content

Instantly share code, notes, and snippets.

@congthien
Created September 10, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save congthien/1b1a05396cc42486c707e85054c990bc to your computer and use it in GitHub Desktop.
Save congthien/1b1a05396cc42486c707e85054c990bc to your computer and use it in GitHub Desktop.
<?php
class Wpc_Posts extends WP_Widget {
public function __construct() {
// Widget actual processes
// parent::__construct(
// 'wpc_posts', // Base ID
// __('WPC Posts','wpcharming'), // Name
// array( 'description' => __( 'Eye catching posts widget', 'wpcharming' ), ) // Args
// );
$widget_ops = array(
'classname' => '',
'description' => __( 'Eye catching posts widget', 'wpcharming' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct( 'wpc_posts', __( 'WPC Posts', 'wpcharming' ), $widget_ops );
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 4;
$post_order = isset( $instance['post_order'] ) ? $instance['post_order'] : 'date';
$post_order_types = array(
'comment_count' => 'Comments',
'date' => 'Date',
'rand' => 'Random'
);
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:','wpcharming'); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php echo __('How many posts to show ?' ,'wpcharming') ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'post_order' ) ); ?>"><?php _e('Posts order:', 'wpcharming') ?></label>
<select class="widefat" name="<?php echo esc_attr( $this->get_field_id( 'post_order' ) );?>" id="<?php echo esc_attr( $this->get_field_id( 'post_order' ) );?>">
<?php foreach ( $post_order_types as $post_order_type=>$post_order_value ) { ?>
<option value="<?php echo esc_attr($post_order_type); ?>" <?php selected( $post_order_type, $post_order);?>><?php echo esc_attr($post_order_value); ?></option>
<?php } ?>
</select>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
$instance = array();
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];;
$instance[ 'post_order' ] = $new_instance[ 'post_order' ];
return $instance;
}
public function widget( $args, $instance ) {
// Outputs the content of the widget
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$post_order = isset( $instance['post_order'] ) ? $instance['post_order'] : 'date';
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 4;
if ( ! $number ) {
$number = 4;
}
echo $args['before_widget'];
// Widget title
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $number,
'orderby' => $post_order
);
$the_query = new WP_Query( $args );
$count = 0;
if ( $the_query->have_posts() ) :
echo '
<ul class="widget-posts-list">';
while ( $the_query->have_posts() ) : $the_query->the_post();
$count ++;
?>
<li class="<?php if ( $count %2 == 0 ) echo 'light-bg'; ?>">
<?php
if( has_post_thumbnail() ) { the_post_thumbnail( 'thumbnail' ); }
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<div class="clear"></div>
</li>
<?php
endwhile;
echo '
</ul>';
endif;
wp_reset_postdata();
echo $args['after_widget'];
}
}
register_widget( 'Wpc_Posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment