<?php //Gets category info global $wp_query; $cats = get_the_category(); $tempQuery = $wp_query; $currentId = $post->ID; //related category posts $catlist = ""; forEach( $cats as $c ) { if( $catlist != "" ) { $catlist .= ","; } $catlist .= $c->cat_ID; } $newQuery = "posts_per_page=6&orderby=rand&cat=" . $catlist; query_posts( $newQuery ); $categoryPosts = ""; $count = 0; if (have_posts()) { while (have_posts()) { the_post(); if( $count<5 && $currentId!=$post->ID) { $count++; $categoryPosts .= '<li><a href="' . get_permalink() . '">' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '</a><br /> <p class="title">' . the_title( "", "", false ) . '</p></li>'; } } } $wp_query = $tempQuery; ?> <ul class="post-list"> <?php echo $categoryPosts; ?> </ul>