WordPress添加侧栏随机文章小工具教程

WordPress添加侧栏随机文章小工具教程,这个功能对seo来说是个好事,对用户来说,好的文章的展现机会更大,今天风酷seo免费分享为WP主题添加随机文章小工具方法:

首先找到主题的functions.php在?>之前添加如下代码并上传覆盖到主题目录:

WordPress添加侧栏随机文章小工具教程

//随机文章class RandomPostWidget extends WP_Widget{ function RandomPostWidget() { parent::WP_Widget('bd_random_post_widget', '随机文章', array('description' => '我的随机文章小工具') ); } function widget($args, $instance) { extract( $args ); $title = apply_filters('widget_title',empty($instance['title']) ? '随机文章' : $instance['title'], $instance, $this->id_base); if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) { $number = 10; } $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true,'post_status' => 'publish', 'ignore_sticky_posts' => true, 'orderby' =>'rand')); if ($r->have_posts()) { echo $before_widget; if ( $title ) echo $before_title . $title . $after_title;?> <ul> <?php while ($r->have_posts()) : $r->the_post(); ?> <li> <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a> </li> <?php endwhile; ?> </ul> <?php echo $after_widget; wp_reset_postdata(); } }function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; return $instance;}function form($instance){ $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $number = isset($instance['number']) ? absint($instance['number']) : 10;?> <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'); ?>" type="text" value="<?php echo $title; ?>" /></p> <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('文章显示数量:'); ?></label> <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p><?php}}add_action('widgets_init', create_function('', 'return register_widget("RandomPostWidget");'));

转载请注明出处::【风酷SEO】 » WordPress添加侧栏随机文章小工具教程

评论 0


Fatal error: Call to undefined function hui_user_avatar() in D:\wwwroot\fengkunet\wwwroot\wp-content\themes\xiu\functions.xiu.php on line 788