wordpress get random post

You can get current wordpress category get_the_category function.

Random Post Code

<?php
global $post;
$categories = get_the_category($post->ID);
$catId=$categories[0]->term_id;//wordpress current category
?>
<ul>
<?php
$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; ?>
</ul>
 

Leave a Reply

Your email address will not be published. Required fields are marked *