首页 > CMS应用 > 正文
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' );

 

第二步:导航栏加入

<a href=”<?php echo get_option(‘home’) ?>/?random” title=”随机推荐一篇文章给你!”>随机</a>

有自定义菜单的主题可以自定义一个链接加入菜单,url:/?random,便签随你意愿随便填就可以了


OK, 这样就完成了,功能的话就是每点击一次都会随机不同的文章,阿门!

				

文章评论

共有 2 条评论

  1. 许小曼

    [不活了]

  2. 许小曼

    此贴有意思~

Top