WordPress添加随便看看

前端之家收集整理的这篇文章主要介绍了WordPress添加随便看看前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

随便看看顾名思义就是随机给出文章看看,就是本博客菜单栏上的随便看看的功能。很小但很实用。

原理:

操作方法

$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://www.f2er.com/wordpress/67710.html

猜你在找的wordpress相关文章