I'm trying to find a way to display related posts that are in a subcategory of a specific parent category based upon the subcategory of the current post. The current post may be assigned to multiple parent categories, but I only want to display related posts of one of the parent category's subcategories.
So for example, a post may be assigned to categories A, B and C, but I want to show related posts based upon the current post's B subcategory and not show all the posts from either A, B or C. Sorry, this question/problem is hard to explain.
I'm using this code to display the category name and posts, but I'm not sure how to exclude parent categories from it, so that only the subcategory posts will display.
More in
<?php $category = get_the_category();
if ( in_category(52) || in_category(56) || in_category(57) || in_category(99) || in_category(28) ) {
echo $category[1]->cat_name;
} else {echo $category[0]->cat_name;}
?>
<?php if (have_posts() && !(in_category('32'))) : ?>
<?php $i = 1; while (have_posts() && $i <= 1) : the_post(); ?>
<?php $related = get_posts(array('category__in' => wp_get_post_categories($post->ID), 'numberposts' => 1, 'post__not_in' => array($post->ID)));
if($related) foreach( $related as $post) {
setup_postdata($post); ?>
<?php the_post_thumbnail('medium'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<a href="<?php the_permalink(); ?>"><?php the_excerpt(''); ?></a>
<?php the_author(); ?></div>
<?php } wp_reset_postdata(); ?>
<?php $i++; endwhile; ?>
<?php endif; ?>
Thanks!