Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Display category post list based on the current posts category

Does anyone know how I can display a list of WordPress posts based on the current post pages current category?

For example if I'm currently on post333.html & the category the page falls under is "Drinks".

How would I get the rest of the posts under that category to display in a list as 'suggested topics' on post333.html.

And if the post & category changes so will the list.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The following code should go directly into your template file.

<?php
// Get Other posts in same category 
$suggested_posts = get_posts( array( 
    'category__in' => wp_get_post_categories( $post->ID ), // Retrieve the list of categories for a post.
    'numberposts' => 5, // Number of posts
    'post__not_in' => array( $post->ID ) // Exclude current post
) );

// If post found
if( $suggested_posts ) {
    foreach( $suggested_posts as $post ) {
        setup_postdata($post); ?>
        <ul> 
            <li>
            <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;'); ?>
            </li>
        </ul>   
    <?php }
}
wp_reset_postdata(); 
?>

If you want to use it anywhere then you may create a shortcode like below.

<?php
function yourprefix_suggested_posts( $atts ){
    // Get Other posts in same category 
    $suggested_posts = get_posts( array( 
        'category__in' => wp_get_post_categories( $post->ID ), // Retrieve the list of categories for a post.
        'numberposts' => 5, // Number of posts
        'post__not_in' => array( $post->ID ) // Exclude current post
    ) );

    // If post found
    if( $suggested_posts ) {
        foreach( $suggested_posts as $post ) {
            setup_postdata($post); ?>
            <ul> 
                <li>
                <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;'); ?>
                </li>
            </ul>   
        <?php }
    }
    wp_reset_postdata();    
}
add_shortcode( 'suggested_posts', 'yourprefix_suggested_posts' );
?>

Later you can use [suggested_posts] shortcode anywhere.

(Not tested but it should work.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!