用户登录
还没有账号?立即注册
用户注册
点击换图
投稿取消
文章分类:
还能输入300字

上传中....

随机显示文章

时间:2023-03-30 作者: 来源:顿格文库

WordPress现在使用于各种各样的网站,博客,商务站,论坛站等等,功能强大地球人都知道,就不赘述了。

分享一段代码,可以把访客转到随机文章的页面。

首先把下面这段代码添加到functions.php中。

add_action('init','random_add_rewrite');

function random_add_rewrite() {

global $wp;

$wp->add_query_var('random');

add_rewrite_rule('random/?$', 'index.php?random=1', 'top');

}

add_action('template_redirect','random_template');

function random_template() {

if (get_query_var('random') == 1) {

$posts = get_posts('post_type=post&orderby=rand&numberposts=1');

foreach($posts as $post) {

$link = get_permalink($post);

}

wp_redirect($link,307);

exit;

}

}

然后创建一个页面链接为http://yourdomain.com/random 。当用户点击之后就被转接到你博客的随机文章页面。

另外如果你使用W3 Total Cache插件了的话,要在exclude list添加以下代码才能成功。

/random/

/index.php?random=1



『此①文为顿格文库②文章,不得自行转载,违者必追究!』

热门关键词