Skip to content

Instantly share code, notes, and snippets.

@coreyweb
Created May 11, 2012 01:32
Show Gist options
  • Save coreyweb/2656938 to your computer and use it in GitHub Desktop.
Save coreyweb/2656938 to your computer and use it in GitHub Desktop.
Show a random post from a category, excluding the current post
<?php // show a random video from the last 10 posts in the video category
$vidCount = rand(0, 9);
$my_query = new WP_Query(
array(
"cat" => 11,
"showposts" => 1,
"offset" => $vidCount,
"post__not_in" => array($post->ID) // exclude the current post from the list
)
);
while ($my_query->have_posts()) : $my_query->the_post();
// get the medium version of the thumbnail image (i.e. 300x200)
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
?>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>">
<img src="<?php echo $src[0]; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
</a>
<?php } ?>
<?php endwhile; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment