WordPress加入”随便看看”菜单功能

admin

发表文章数:3099

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\' );

使用方法

访问你网站的域名https://wuzuhua.cn/?random即可看到效果,如果要在导航菜单现实的话,在后台新建一个自定义URL菜单即可!

未经允许不得转载:作者:admin, 转载或复制请以 超链接形式 并注明出处 夜河资源网
原文地址:《WordPress加入”随便看看”菜单功能》 发布于2019-12-15

分享到:
赞(0) 打赏

评论 抢沙发

3 + 9 =


撰写不易~如果您觉得文章对您有帮助的话可以打赏我哦~谢谢亲亲~

支付宝扫一扫打赏

微信扫一扫打赏

外服网游加速器破解版
包含腾讯网游加速器、海豚网游加速器等八款知名加速器,可加速外服游戏,永久包售后包更新,不定时添加新款破解版加速器!
切换注册

登录

忘记密码 ?

切换登录

注册