File "widgets.php"
Full Path: /var/www/bvnghean.vn/wp-content/themes/bvnghean/include/widgets.php
File size: 3.29 KB
MIME-type: text/x-php
Charset: utf-8
<?php
// Creating the widget
class cat_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'cat_widget',
// Widget name will appear in UI
'Category Widget',
// Widget description
array( 'description' => 'Hiển thị bài viết theo category', )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$post_number = $instance['post_number'];
$cat_id = $instance['cat_id'];
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
$new_query = new WP_Query(array(
'posts_per_page' => $post_number,
'cat' => $cat_id
));
if ($new_query->have_posts()) : while( $new_query->have_posts() ) : $new_query->the_post(); ?>
<?php
$thumb = get_image_src(get_post_thumbnail_id(),'medium');
?>
<a class="post-item color-hover-second mb-20" href="<?=the_permalink()?>">
<div class="post-thumbnail" style="background-image: url('<?=$thumb?>')"></div>
<div class="p-15">
<p href="" class="color-main color-hover-second post-title font-h6"><?=the_title()?></p>
</div>
</a>
<?php endwhile;
endif;
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
$default = array(
'title' => '',
'post_number' => 3,
'cat_id' => 0
);
$instance = wp_parse_args( (array) $instance, $default );
$title = esc_attr($instance['title']);
$post_number = esc_attr($instance['post_number']);
$cat_id = esc_attr($instance['cat_id']);
// Widget admin form
?>
<p>Tiêu đề <input class="widefat" id="<?=$this->get_field_id( 'title' )?>" name="<?=$this->get_field_name( 'title' )?>" type="text" value="<?=$title?>" /><p>
<p>Danh mục
<select class="widefat" name="<?=$this->get_field_name('cat_id')?>">
<option value="">Chọn danh mục</option>
<?php
$categories = get_categories();
foreach($categories as $category) {
$selected = '';
if($category->term_id==$cat_id) $selected = 'selected';
echo '<option value="'.$category->term_id.'" '.$selected.'>'.$category->name.'</option>';
}
?>
</select>
</p>
<p>Số lượng bài viết hiển thị <input type="number" class="widefat" name="<?=$this->get_field_name('post_number')?>" value="<?=$post_number?>" max="30" /></p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['post_number'] = strip_tags($new_instance['post_number']);
$instance['cat_id'] = strip_tags($new_instance['cat_id']);
return $instance;
}
} // Class cat_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'cat_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
?>