网站添加一个随机预览文章随便看看功能

1、这个功能是和随机预览文章的功能,点击一次更换一次文章

2、将下面的 PHP 代码加入到主题目录下:themes/zibll/functions.php 文件中。

3、添加完成之后访问:你的域名+/?random,然后可以自己添加在菜单里面

// 添加随便看看(BY www.521cd.cn)
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' );
// 随便看看结束(BY www.521cd.cn)
供学习和研究使用,禁止商业用途
购买前可以联系作者确认资源信息,防止交易矛盾
© 版权声明
THE END
喜欢就支持一下吧
点赞10
评论 抢沙发

请登录后发表评论

    暂无评论内容