Could someone tell me how i could make this work? i have a loop, and i would like to create another loop to find the other posts from the same category : but this does not work :

<?php while ( have_posts() ) : the_post(); ?>
            <?php /* How to display all other posts. */ ?>


                $monid = the_ID();
                $cat = get_the_category($monid);  //this works
                $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat );
                $myposts = get_posts( $args );
                var_dump($myposts); //this don't ?>


i found this topic Loop inside the loop but it does not really work for me : any help?

thanks a lot

2 Answers 2


the_ID() prints the current post's ID. You need to use get_the_ID() to return it.

Also, get_the_category returns an array of category objects - one foreach category term the post belongs to.

The 'category' attribute expects a category term ID. So you need to pick a category object, then obtain its ID:

  $cats = get_the_category();
  $cat_obj = array_shift($cats);
  $cat_id = (int) $cat_obj->cat_ID;


$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat_id );
  • Thanks a lot Stephen, so cat_id works, but $myposts = get_posts( $args ); does not work: it returns an empty array ... Do know why?
    – Paul
    May 18, 2012 at 17:27

Okay, it works like that :

<?php while ( have_posts() ) : the_post(); ?>

                    $cats = get_the_category();
                    $cat_obj = array_shift($cats);

                    $cat_id = (int) $cat_obj->cat_ID;

                    $qq = query_posts( 'cat=$cat_id&posts_per_page=1' );

                    foreach($qq as $q2){


i hope this can help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.