wordpress 自定义 widgets

<?php
//1.创建一个“区块”,用来挂在widget
register_sidebar(array(
    'name' => 'Widget Area ONE',
    'before_widget' => '<div class="widget-sidebar f-tweets">',
    'after_widget' => '</div>',
    'before_title' => '<h3 class="widget-title f-tweets-title">',
    'after_title' => '</h3>'
));
?>

<?php
//2。自定义一个wedget
register_widget('Video_Widget');
class Video_Widget extends WP_Widget {
	function Video_Widget() {
		parent::WP_Widget(false, $name = 'Video widget');	
	}

	function widget($args, $instance) {
		extract( $args );
		global $wpdb;
		$count = $instance['count'];
		$r = array(
			'numberposts' => $count, 
			'category' => 3,
			'orderby' => 'post_date',
			'order' => 'DESC', 
			'post_type' => 'post',
		);
		$posts = get_posts($r);
		if( empty($posts) ) return;
		
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . apply_filters('the_title', $instance['title'])
                      . $after_title; ?>
				<ul class="flowwidgets">
				<?php
				foreach($posts as $post){
					setup_postdata($post);
					$attachment_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
					$url = $attachment_url['0'];
					echo '<li><a><img src="'.$url.'" /></a><h2>'.get_the_title($post->ID).'</h2>'.the_content($post->ID).'</li>';
				}
				?>
				</ul>
              <?php echo $after_widget; ?>
        <?php
	}

	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	function form($instance) {
		$title = esc_attr($instance['title']);
		$count = esc_attr($instance['count']);
        ?>
            <p>
				<label for="<?php echo $this->get_field_id('title'); ?>">
					<?php _e('Title:'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
				</label>
			</p>
            <p>
				<label for="<?php echo $this->get_field_id('count'); ?>">
					<?php _e('Display Number:'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" />
				</label>
			</p>
        <?php 
	}
}
?>


3.调用
<?php
 dynamic_sidebar( 'Widget Area ONE' );
 ?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值