首页上显示一个随机文章小工具对SEO很有好处。可以用插件实现,也可以不用插件实现。用插件的话,比较消耗服务器资源。直接用代码简单直接一些。
1. 在当前使用的wordpress主题文件夹下面新建一个random-post.php文件,utf-8编码。拷贝如下代码进去:
<?php /* * Plugin Name: 随机文章 小工具 * Description: 这是一个显示随机文章的小工具(widget),可以设置随机文章的数量,指定文章分类目录。 */ function the_rand_posts($args = ''){ $default = array('showPosts'=>10, 'cat'=>'0', 'class'=>'randomPosts'); $r = wp_parse_args($args,$default); extract($r); $rand_query = new WP_Query("cat=$cat&showposts=$showPosts&orderby=rand"); if($rand_query->have_posts()){ echo '<ul>'; while($rand_query->have_posts()){ $rand_query->the_post(); echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; }/*End while($rand_query->have_posts())*/ echo '</ul>'; }/*End if($rand_query->have_posts())*/ } /* ** 定义一个显示随机文章的小工具(widget),以便在控制面板在小工具中进行管理 ** 该类必须是WP_Widget在扩展类 ** wordpress v2.8 之后可用 */ class RandomPostsWidget extends WP_Widget { /* ** 构造函数 ** 声明一个数组$widget_ops,用来保存类名和描述,以便在控制面板正确显示工具信息 ** $control_ops 是可选参数,用来定义小工具在控制面板显示的宽度和高度 ** 最后是关键的一步,调用WP_Widget来初始化我们的小工具 */ function RandomPostsWidget(){ $widget_ops = array('classname'=>'widget_random_posts','description'=>'随机显示博客中的文章'); $control_ops = array('width'=>200,'height'=>300); $this->WP_Widget(false,'随机文章',$widget_ops,$control_ops); } /* ** 定义form函数 ** 用来显示小工具在控制面板的选项 ** 这个随机文章小工具可以让用户自定义4个内容: ** 模块标题,默认为“随机文章” ** 显示文章数量,默认为10篇 ** 分类目录ID,默认为0,即显示所有分类下的文章 ** CSS样式class名,默认为randomPosts */ function form($instance){ $instance = wp_parse_args((array)$instance,array('title'=>'随机文章','showPosts'=>10,'cat'=>0,'class'=>'randomPosts')); $title = htmlspecialchars($instance['title']); $showPosts = htmlspecialchars($instance['showPosts']); $cat = htmlspecialchars($instance['cat']); $class = htmlspecialchars($instance['class']); echo '<p style="text-align:left;"><label for="'.$this->get_field_name('title').'">标题:<input style="width:200px;" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" /></label></p>'; echo '<p style="text-align:left;"><label for="'.$this->get_field_name('showPosts').'">文章数量:<input style="width:200px;" id="'.$this->get_field_id('showPosts').'" name="'.$this->get_field_name('showPosts').'" type="text" value="'.$showPosts.'" /></label></p>'; echo '<p style="text-align:left;"><label for="'.$this->get_field_name('cat').'">分类ID:<input style="width:200px" id="'.$this->get_field_id('cat').'" name="'.$this->get_field_name('cat').'" type="text" value="'.$cat.'" /></label></p>'; echo '<p style="text-align:left;"><label for="'.$this->get_field_name('class').'">class:<input style="width:200px" id="'.$this->get_field_id('class').'" name="'.$this->get_field_name('class').'" type="text" value="'.$class.'" /></label></p>'; } /* ** 定义update函数,保存设置 */ function update($new_instance,$old_instance){ $instance = $old_instance; $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['showPosts'] = strip_tags(stripslashes($new_instance['showPosts'])); $instance['cat'] = strip_tags(stripslashes($new_instance['cat'])); $instance['class'] = strip_tags(stripslashes($new_instance['class'])); return $instance; } /* ** 定义widget函数,用于在网页中显示 */ function widget($args,$instance){ extract($args); $title = apply_filters('widget_title',empty($instance['title']) ? ' ' : $instance['title']); $showPosts = empty($instance['showPosts']) ? 10 : $instance['showPosts']; $cat = empty($instance['cat']) ? 0 : $instance['cat']; $class = empty($instance['class']) ? 'randomPosts' : $instance['class']; echo $before_widget; echo $before_title . $title . $after_title; the_rand_posts("showPosts=$showPosts&cat='$cat'&class='$class'"); echo $after_widget; } } // RandomPostsWidget 类定义结束 /* ** 注册小工具 */ function RandomPostsInit(){ register_widget('RandomPostsWidget'); } add_action('widgets_init','RandomPostsInit'); ?>
第二步:编辑wordpress主题文件夹内的functions.php,在functions.php里添加代码
require_once(‘random-post.php’);
第三步:在wordpress控制板的“外观”=>“小工具”里面找到“随机文章”小工具,添加到侧边栏里就大功告成了!
One thought on “简单三步实现wordpress不用插件的侧边栏随机文章小工具功能”