0

I know I'm doing something that's been done 1000's of times before in WordPress, but struggling to get the terms to find it on the Interwebs.

I've got a custom category template page and have a while loop that shows the first 5 results (of which there are more than 5).

How do I output something within my while that shows the relative position in the results and the total number:

I know how to get the post count:

<?php
   $catcount = new WP_Query( 'cat=4&posts_per_page=-1' );
?>

But not sure how to setup the pagination and the relative results, meaning

Results 1-10 of <?php echo $total->found_posts; ?>

How do I get the results values 1 of 2 and then create pagination for subsequent pages?

1

it's been a long time, I had exact the same task and here is the solution

$from = ($query->query_vars['posts_per_page'] * $paged) - ($query->query_vars['posts_per_page'] - 1);
if(($query->query_vars['posts_per_page'] * $paged) <= ($query->found_posts)){
  $to = ($query->query_vars['posts_per_page'] * $paged);
}else{
  $to = $query->found_posts;
}
if($from == $to){
  $from_to = $from;
}else{
  $from_to = $from.' - '.$to;
}

It's not the perfect code, but it works in most of situation. Have a nice day!)

  • Is this some code from inside a callback hooked to some action or filter? – kaiser Mar 20 '17 at 15:51
  • no, it's code for custom query – Sergey Serduk Mar 20 '17 at 18:22
  • 1
    Yes, but it has to run somewhere. – kaiser Mar 20 '17 at 20:11
1

If you are working with the default WP_Query you can do something like in your loop:

if ( have_posts() ) {
    global $wp_query;
    $paged = !empty($wp_query->query_vars['paged']) ? $wp_query->query_vars['paged'] : 1;
    $prev_posts = ( $paged - 1 ) * $wp_query->query_vars['posts_per_page'];
    $from = 1 + $prev_posts;
    $to = count( $wp_query->posts ) + $prev_posts;
    $of = $wp_query->found_posts;

    // ... Display the information, for example
    printf( '%s to %s of %s', $from, $to, $of );

    // Then your normal loop
    while ( have_posts() ) {
        //...
    }
}

If you are working with a custom query you could do it like this:

$myquery = new WP_Query($yourqueryargs);
if ( $myquery->have_posts() ) {
    $paged = !empty($myquery->query_vars['paged']) ? $myquery->query_vars['paged'] : 1;
    $prev_posts = ( $paged - 1 ) * $myquery->query_vars['posts_per_page'];
    $from = 1 + $prev_posts;
    $to = count( $myquery->posts ) + $prev_posts;
    $of = $myquery->found_posts;

    // ... Display the information, for example
    printf( '%s to %s of %s', $from, $to, $of );

    // Then your normal loop
    while ( have_posts() ) {
        //...
    }
}

Even better - Create a function that you can use either with the global $wp_query or with any custom query.

function myThemePostsNow ( $query = null ) {
    global $wp_query;

    if ( isset($query) && !($query instanceof WP_Query) ) {
        return; // Cancel if a query has been set and is not a WP_Query instance
    }
    elseif ( !isset($query) ) {
        $query = $wp_query; // If $query is not set, use global $wp_query data
    }

    if ( ! $query->have_posts() ) {
        return; // Cancel if $query don't have posts
    }

    // If we made it here it means we have a WP_Query with posts

    $paged = !empty($query->query_vars['paged']) ? $query->query_vars['paged'] : 1;
    $prev_posts = ( $paged - 1 ) * $query->query_vars['posts_per_page'];
    $from = 1 + $prev_posts;
    $to = count( $query->posts ) + $prev_posts;
    $of = $query->found_posts;

    // Return the information
    return sprintf( '%s to %s of %s', $from, $to, $of );
}

Then just call your function

if ( have_posts() ) {

    echo myThemePostsNow();

    while ( have_posts() ) {
        //...
    }
}

or in a custom query like:

if ( $myquery->have_posts() ) {

    echo myThemePostsNow();

    // Then your normal loop
    while ( have_posts() ) {
        //...
    }
}
-2

Ok so, This following is how you can have custom wp query with pagination, I also suggest you install this plugin: https://wordpress.org/plugins/wp-pagenavi/ This makes life easier :) Once you install this plugin, you can add this code to a custom page template, archive pages, category page in your theme.

        <?php
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
        $query_args = array(
            'post_type' => 'post', //custom post type as well
            'posts_per_page' => 5, //number of posts per page
            'paged' => $paged,
            'cat' => $catID, //category ID
        );
        $magic_all_posts_query = new WP_Query( $query_args );
        ?>     
        <?php
        if ( $cn_all_posts_query->have_posts() ) : while ( $magic_all_posts_query->have_posts() ) : $cn_all_posts_query->the_post();
        ?>
        <div class="entry-single <?php echo $class ?>">
             <h1 class="entry-title">
                  <a href="<?php the_permalink() ?>">
                       <?php the_title(); ?>
                  </a>
             </h1>
             <div class="entry-content">
                  <?php
                       $content = get_the_content();
                  ?>
             </div>
        </div>
        <?php endwhile;
            wp_pagenavi( array( 'query' => $cn_all_posts_query ) );
        ?>

Do let us know if you run into any roadblocks with this.

  • Sorry.. new to this, can you give more info? – Keefer Sep 11 '15 at 16:21

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.