• Hi,

    I’m trying to display a list of related posts belonging to the current child category, excluding the parent category, and the current post.

    Hoping the code below can provide a start:

    <ul>
    
    <?php $related = get_posts(  array( 'post_type' => 'post', 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 40, 'orderby' => 'date', 'order' => 'DESC' ) );
    
    if( $related ) foreach( $related as $post ) :
    setup_postdata($post); ?>
    
    <li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="current"'; }
    else {} ?>>
    
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    
    </li>
    <?php endforeach; ?>
    
    </ul>

The topic ‘Related posts in current child category’ is closed to new replies.