1

Maybe there's a better way to do this. Let me explain what I have right now:

I have a custom post type (Products), and a custom taxonomy (Manufacturers).

What I have outputting so far:

Manufacturer Name 1
Product 1
Product 2
Product 3

Manufacturer Name 2
Product 1
Product 2
Product 3

This is great, except pagination is being ignored. All 20+ products are being listed, BUT the page links are working.

My code:

<ul class="product-listing list-unstyled">
    <?php

    $categories = get_terms('manufacturer');

    foreach($categories as $category) : ?>
    <li class="row">
        <div class="col-xs-12">
            <h3><?php echo $category->name; ?></h3>
        </div>
    </li>

    <?php
    $posts = get_posts(array(
        'post_type' => 'product',
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,
        'posts_per_page' => 10,
    ));
        foreach($posts as $post):
        setup_postdata($post); ?>
        <li class="row">
            <div class="col-xs-12 product-copy">
                <h5><a href="<?php the_permalink(); ?>">
                    <?php echo $category->name.' - '.get_the_title(); ?>
                </a></h5>
                <p><?php echo wp_trim_words( get_the_content(), 35 ); ?> <a href="<?php the_permalink(); ?>">Read More</a></p>
            </div>
        </li>
    <?php endforeach; ?>
<?php endforeach; ?>
</ul>

I've been staring at this code for too long, and trying different solutions. Any tips?

2 Answers 2

0

You have to use $paged variable like this :

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = get_posts(array(
        'post_type' => 'product',
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,
        'posts_per_page' => 10,
        'paged' => $paged
    ));
3
  • Thanks for your reply Bindiya. I added the paged lines to my code, but its still displaying all posts. I changed the 'posts_per_page' to 3 as well (to test), and it had no effect - still shows all. Any ideas?
    – bmac
    Commented May 6, 2014 at 13:15
  • Bindiya - after thinking about this, it seems turning manufacturers into its own custom post type is probably the way to go. I'll have to do some digging into this.
    – bmac
    Commented May 6, 2014 at 20:12
  • You have to set both posts_per_page and paged Commented May 7, 2014 at 7:45
0

Pagination would not work automatically for get_posts() function until you added $paged variable.

2
  • Exactly what was proposed in the first answer. Your answer is useless as an answer with some code to support what your saying. As your answer stands, it should be added as a comment, not an answer, -1 Commented May 6, 2014 at 6:27
  • 1
    Oh, it's okay.. That was the first time I was answered on WPSE. That's why I actually did not know when I have to post a comment or an answer. Thanks for this concern. :)
    – Saikat
    Commented May 6, 2014 at 6:35

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.