
凯发app凯发app官网首页加入一个可以“随机访问”热门博客的功能,其实这个就是类似谷歌凯发app凯发app官网首页的那个“手气不错”。所以就改为随机访问某一篇文章,今天顺便把实现办法分享给大家。
我们平时所说的wordpress导航栏上的手气不错的按钮功能,其实就是点击一下,将会随机显示篇文章的详细内容,也就是所谓的文章页。这个功能比较合适老用户访问站点旧文章,因为这种无法预测的随机感,能给人带来一定的新鲜度,所以往前方也跟着上线了这个功能,希望大家喜欢。
在wordpress导航栏上实现手气不错的按钮功能,有很多种方法,今天本站只分享最简单一种,只需要简单的两步即可,具体如下:
第一步,把以下代码放到functions.php的最后一个?>前面:
//导航菜单添加手气不错按钮
function random_postlite() {
global $wpdb;
$query = "select id from $wpdb->posts where post_type = 'post' and post_password = '' and post_status = 'publish' order by rand() limit 1";
if ( isset( $_get['random_cat_id'] ) ) {
$random_cat_id = (int) $_get['random_cat_id'];
$query = "select distinct id from $wpdb->posts as p inner join $wpdb->term_relationships as tr on (p.id = tr.object_id and tr.term_taxonomy_id = $random_cat_id) inner join $wpdb->term_taxonomy as tt on(tr.term_taxonomy_id = tt.term_taxonomy_id and taxonomy = 'category') where post_type = 'post' and post_password = '' and post_status = 'publish' order by rand() limit 1";
}
if ( isset( $_get['random_post_type'] ) ) {
$post_type = preg_replace( '|[^a-z]|i', '', $_get['random_post_type'] );
$query = "select id from $wpdb->posts where post_type = '$post_type' and post_password = '' and post_status = 'publish' order by rand() limit 1";
}
$random_id = $wpdb->get_var( $query );
wp_redirect( get_permalink( $random_id ) );
exit;
}
if ( isset( $_get['random'] ) )
add_action( 'template_redirect', 'random_postlite' );
ps:代码来源于
第二步,在wordpress后台》外观》菜单中添加一个链接类型的菜单,在url中输入:https://qianfangzy.com/?random(记得把本站域名改为贵站域名哦),在链接文本中输入:手气不错(或其他)。
至此,已经成功在wordpress导航栏添加手气不错的按钮功能,具体效果大家可以试试本站菜单栏的“”按钮。

![软件推荐[wordpress]wp缓存加速插件wp rocket 3.10.1免授权版-哎呦不错往前方资源网](https://img.qianfangzy.com/i/2023/02/03/132446-0.webp)





