List posts from same category
This goes inside the loop on single.php – This lists all the posts in the same category as the current post.
/** * Snippet Name: List posts from same category * Snippet URL: https://wpcustoms.net/snippets/list-posts-from-same-category/ */ <?php if ( is_single()) { $categories = get_the_category(); if ($categories) { foreach ($categories as $category) { $cat = $category->cat_ID; $args=array( 'cat' => $cat, 'order' => DESC, 'orderby' => rand, 'post__not_in' => array($post->ID,56), 'posts_per_page' => 55, 'caller_get_posts' => 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo '<p> '. $category->cat_name . '</p>'; while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></p></a> <?php endwhile; } //if ($my_query) } //foreach ($categories } //if ($categories) wp_reset_query(); // Restore global post data stomped by the_post(). } //if (is_single()) ?>