0

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

1

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 );
  • 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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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