Quantcast
Channel: User Michael - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 44

Answer by Michael for How to display related posts from the same category?

$
0
0

One possibility:

$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);        /*whatever you want to output*/    }    wp_reset_postdata();}

Reference:

Answer re-written based on WP_Query():

$related = new WP_Query(    array('category__in'   => wp_get_post_categories( $post->ID ),'posts_per_page' => 5,'post__not_in'   => array( $post->ID )    ));if( $related->have_posts() ) {     while( $related->have_posts() ) {         $related->the_post();         /*whatever you want to output*/    }    wp_reset_postdata();}

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>