php – How to display related posts from parent category


I am using the following code to display the related posts but it is showing posts for that category only like brand showing brand posts only. What I am looking for is if I select any of the posts under child (brand, advertising, online etc), it should display all posts under marketing so I will not have to assign multiple categories to the posts

  • Subject
    • Marketing(3)
      • Brand(1)
      • Advertising(1)
      • Online(1)

$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php the_content('Read the rest of this entry &raquo;'); ?>

<?php } wp_reset_postdata(); ?>

stack kid 6 years 2017-02-24T04:14:29-05:00 0 Answers 0 views 0

Leave an answer