Widget này sẽ giúp chúng ta lấy ảnh của 1 User ID của Flickr và hiển thị lên website. Hình ảnh minh họa như trên. Rất tiện lợi phải không nào?
Tạo Flickr Widget
Chúng ta tạo lấy 1 file có tên flickr_widget.php (hoặc với bất kỳ tên nào bạn thích). rồi để ở theme của bạn đang dùng (bất kỳ chỗ nào trong theme). Sau đó include file vừa tạo vào file functions.php của theme là được.
<?php
class svl_widget_flickr extends WP_Widget {
function svl_widget_flickr() {
$widget_ops = array( 'classname' => 'svl_widget_flickr', 'description' => __('A widget that displays brochure item') );
$this->WP_Widget( 'svl_widget_flickr', __('[SVL] Flickr'), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args );
global $post;
$title = apply_filters('widget_title', $instance['title'] );
$gallery_id = $instance['gallery_id'];
$gallery_items = $instance['gallery_items'];
if ($gallery_items <= 0 ){ $gallery_items = 9; }
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
if ($gallery_id){
echo '<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count='.$gallery_items.'&display=latest&size=s&layout=x&source=user&user='.$gallery_id.'"></script>';
echo '<style>.flickr_badge_image {display: inline-block;margin: 0 5px 5px 0;}</style>';
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['gallery_id'] = strip_tags( $new_instance['gallery_id'] );
$instance['gallery_items'] = strip_tags( $new_instance['gallery_items'] );
return $instance;
}
function form( $instance ) {
$defaults = array( 'title' => __(null), 'gallery_items' => 9, 'gallery_id' => 0 );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<div class="ewf-meta">
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'gallery_id' ); ?>"><?php _e('Gallery or User ID:'); ?></label>
<input id="<?php echo $this->get_field_id( 'gallery_id' ); ?>" name="<?php echo $this->get_field_name( 'gallery_id' ); ?>" value="<?php echo $instance['gallery_id']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'gallery_items' ); ?>"><?php _e('Number of items:'); ?></label>
<input id="<?php echo $this->get_field_id( 'gallery_items' ); ?>" name="<?php echo $this->get_field_name( 'gallery_items' ); ?>" value="<?php echo $instance['gallery_items']; ?>" style="width:100%;" />
</p>
</div>
<?php
}
}
function svl_flickr_widgets() {
register_widget( 'svl_widget_flickr' );
}
add_action( 'widgets_init', 'svl_flickr_widgets' );
?>
Download flickr widget
Flickr widget
Khi đã làm xong các bạn thử với user ID 95607642@N04 xem thế nào nhé ^^
Chúc các bạn thành công^^

Không có nhận xét nào:
Đăng nhận xét