wordpress 自定义 widgets

来源:互联网 发布:淘宝啥时候能买彩票 编辑:程序博客网 时间:2024/06/10 20:39
<?php//1.创建一个“区块”,用来挂在widgetregister_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。自定义一个wedgetregister_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"><?phpforeach($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' ); ?>


原创粉丝点击