0

In my index page, I want to display 12 posts for each category, so I iterate the categories:

<?php foreach ($sub_cates as $cate) { ?>

    <section class="b">
                <div class="grid">
                    <header class="title">
                        <h2><?php echo $cate->name ?><a class="more" href="<?php echo get_category_link($cate) ?>">More</a></h2>
                    </header>
                    <div class="row">
                        <?php
                        //rewind_posts();
                        query_posts(array(
                            "category" => $cate->term_id,
                            "numberposts" => 12
                        ));
                        $w = $GLOBALS['wp_query'];
                        while (have_posts()) {
                            the_post();
                            ?>
                            <article class="col-xs-4 col-sm-3 col-md-2 ">
                                <?php get_template_part("grid-item") ?>
                            </article>
                            <?php
                        }
                        ?>
                    </div>

                </div>
    </section>
<?php } ?>

However each section print the same collections of posts, even after I use the rewind_posts before the loop.

What's going on?

5
  • is your category name dispaly correctly? Nov 4, 2016 at 5:25
  • Yes, the category name is right, so are the categories links.
    – hguser
    Nov 4, 2016 at 5:33
  • have you tried resetting the query " wp_reset_query(); " ? Nov 4, 2016 at 5:39
  • wp_reset_query does not work.
    – hguser
    Nov 4, 2016 at 5:48
  • have you tried echoing " $cate->term_id " ? does it display correctly? Nov 4, 2016 at 5:53

2 Answers 2

1

Finally, I figure it out.

It seems that the get_posts and query_posts accept different arguments, when I use the get_posts I can setup the arguments like this:

                    get_posts(array(
                        "category" => $cate->term_id,
                        "numberposts" => 12
                    ));

While when I use query_posts I have change that to:

                    query_posts(array(
                        "cat" => $cate->term_id,
                        "numberposts" => 12
                    ));

Notice the category to cat. This is a sad story.:(

0

The job of rewind_posts() is to rewind the same query -- which is not was you want in this case.

Have you tried with get_posts() instead? wp_reset_postdata() is needed for the $post object but wp_reset_query() isn't required after the loop in this case.

<?php foreach ( $sub_cates as $cate ) { ?>

    <section class="b">
        <div class="grid">
            <header class="title">
                <h2><?php echo $cate->name ?><a class="more" href="<?php echo get_category_link( $cate ) ?>">More</a>
                </h2>
            </header>
            <div class="row">
                <?php

                $posts = get_posts( array (
                    "category"    => $cate->term_id, // empty value will query a bunch of posts
                    "numberposts" => 12,
                ) );

                foreach ( $posts as $post_object ) {

                    setup_postdata( $GLOBALS['post'] =& $post_object );

                    ?>
                    <article class="col-xs-4 col-sm-3 col-md-2 ">
                        <?php get_template_part( "grid-item" ) ?>
                    </article>
                    <?php
                }

                wp_reset_postdata(); // like wp_reset_query() but for $post

                ?>
            </div>

        </div>
    </section>
<?php } ?>
2
  • The manner you mentioned in your answer is what I used before, it worked. I just tried to change to the standard loop where I meet this problem.
    – hguser
    Nov 4, 2016 at 5:45
  • You should limit by post type and check if $cate->term_id exists. If you run that category query with an empty value it'll get anything it can.
    – jgraup
    Nov 4, 2016 at 5:54

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.