Đây là ví dụ tạo widget hiển thị bài viết ngẫu nhiên
[php]
class Random_Post extends WP_Widget {
function __construct() {
parent::__construct(
‘random_post’,
‘Bài ngẫu nhiên’,
array( ‘description’ => ‘Widget hiển thị bài viết ngẫu nhiên’ )
);
}
function form( $instance ) {
$default = array(
‘title’ => ‘Tiêu đề widget’,
‘post_number’ => 10
);
$instance = wp_parse_args( (array) $instance, $default );
$title = esc_attr($instance[‘title’]);
$post_number = esc_attr($instance[‘post_number’]);
echo ‘
Nhập tiêu đề ” title=”“>
“;
endif;
echo $after_widget;
}
}
add_action( ‘widgets_init’, ‘create_randompost_widget’ );
function create_randompost_widget() {
register_widget(‘Random_Post’);
}
[/php]