Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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. */ ?>

                <?php

                $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 ?>
                <ul>

                </ul>

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

thanks a lot

share|improve this question
add comment

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;

Then:

$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat_id );
share|improve this answer
    
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 '12 at 17:27
add comment
up vote 0 down vote accepted

Okay, it works like that :

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

                <?php
                    $cats = get_the_category();
                    $cat_obj = array_shift($cats);
                    var_dump($cat_obj);


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

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

                    foreach($qq as $q2){
                        var_dump($q2->post_content);
                    }

                    wp_reset_query();
                ?>

i hope this can help

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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