wordpress get random post

You can get current wordpress category get_the_category function.

Random Post Code

global $post;
$categories = get_the_category($post->ID);
$catId=$categories[0]->term_id;//wordpress current category
$args = array( 'numberposts' => 5, 'orderby' => 'rand' ,'category' =>$catId);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.