Showing x of y posts in custom post type taxonomy WordPress

I’ve created a custom post type, registered it, as well as created and registered an associated custom taxonomy. I need to somehow paginate the category pages, as in, “You are viewing 1-10 of 15 posts in this custom taxonomy”, and then have a link to the next page of these at the bottom.

My loop is working nicely, and is below:

Read More
<div class="simple-product-listing archive-box">

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

<li>

    <div <?php post_class( $classes ); ?>>

        <a title="<?php the_title() ?>" href="<?php the_permalink(); ?>" data-toggle="tooltip">
            <div class="row catalog">
                <div class="col-md-6 col-md-push-6 arrow">

                    <?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
                        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'dnf-cat' );
                        $url = $thumb['0'];
                    ?>

                    <div class="the-prod-image" style="background-image: url(<?=$url?>);"></div>

                    <?php } else { ?>
                    <div class="the-image-div" style="background-image: url(<?php bloginfo('template_directory'); ?>/images/default.svg);"></div>
                    <?php } ?>

                </div>

                <div class="col-md-6 col-md-pull-6 arrow">
                    <h3 class="post-box-title"><?php the_title(); ?></h3>

                    <div class="entry">
                        <i class="blog-title-border"></i>
                        <?php the_excerpt() ?>
                        <div class="btn btn-default btn-sm active text-uppercase" role="button" title="<?php the_title() ?>">Read More &raquo;</div>
                    </div>
                </div>

                <div class="clear"></div>
            </div>
        </a>

    </div>
</li>

<?php endwhile;?>

</div>

All I’ve managed to figure out so far in terms of the pagination display is:

<?php
    $pagenum = $query->query_vars['paged'] < 1 ? 1 :  $query->query_vars['paged'];
    $first = ( ( $pagenum - 1 ) * $query->query_vars['posts_per_page'] ) + 1;
    $last = $first + $query->post_count - 0;
    echo '<p class="results-count">Showing ' . $first . ' of ' . $last . ' products in this category</p>';
?>

But this doesn’t show an “of” number above 1, and doesn’t solve the pagination item that I want for the bottom of the page.

Related posts

1 comment

  1. I found a solution after quite a bit more searching and trial & error…

    <?php
        // Number of products in category
        $pagenum = $query->query_vars['paged'] < 1 ? 1 : $query->query_vars['paged'];
        $first = ( ( $pagenum - 1 ) * $query->query_vars['posts_per_page'] ) + 1;
        $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
        echo '<p class="results-count">Showing ' . $first . ' of ' .count($myposts = get_posts( $args )). ' products in this category</p>';
    ?>
    

Comments are closed.