专业提示-除非需要,否则不要自定义查询。永远不要使用guid
领域
if ( is_singular() && $cats = get_the_category() )
$cat_id = $cats[0]->term_id; // Category ID for current post
elseif ( is_category() )
$cat_id = get_queried_object_id(); // Category ID for current archive
else
$cat_id = 0; // No cats
$posts = get_posts(
array(
\'posts_per_page\' => 1,
\'orderby\' => \'rand\', // Here\'s our random magic
\'cat\' => $cat_id,
)
);
if ( $posts ) {
$random_url = get_permalink( $posts[0]->ID );
}