<?php
/*
Template Name: random
*/
?><?php if ( is_single() || is_page(-19705) ) {
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;
}
$args = array('orderby' => 'rand','showposts' => 1,'cat' => $catid );
$query_posts = new WP_Query();
$query_posts->query($args);
while ($query_posts->have_posts()) : $query_posts->the_post();
?><div style=" background: #e4a10a; text-align: center; width: 91%; margin: auto;font-weight: 700; overflow: auto;">
<a href="<?php the_permalink(); ?>" style=" color: white;">试试手气</a></div>
<?php endwhile;?>
<?php wp_reset_query(); ?><?php } elseif(is_category() ) {
global $post;
$categories = get_the_category();
foreach ($categories as $category) ; ?>
<?php $posts = get_posts('numberposts=1&orderby=rand&category='. $category->term_id);foreach($posts as $post) : ?>
<script> location="<?php the_permalink(); ?>";</script><?php endforeach; ?><?php } else { ?>
<?php $rand_post=get_posts('numberposts=1&orderby=rand'); foreach($rand_post as $post) : ?>
<script> location="<?php the_permalink(); ?>";</script>
<?php endforeach; ?>
<?php ;}?>
然后把这个代表放到文章页和分类页 demo