使用WordPress显示查询结果

I have been searching with no luck so sorry if this has been answered before.

I am wanting to show the results of the query with something like " Viewing 1-10 of 100" basically showing the current posts I am viewing and the total post count. If someone can point me in the right direction I would appreciate it very much.

You can insert the following code in the file search.php or anywhere you need.

<?php
function get_number_of_results( $current_page, $total, $jumps = 10){
    $first_result = 1;
    $start = $first_result;
    $end = $jumps;

    if($current_page >=2) {
        $previous = $current_page - 1;
        $first_result = ($previous * $jumps) + 1;
        $start = $first_result;
        $end = $start + ($total - 1);
    }
    return "$start - $end";
}

$current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$total = $wp_query->post_count; 
// Result
echo "Viewing " . get_number_of_results( $current_page, $total); 
?>

Notes

  • You can move the function to the file functions.php to just call the two las lines of my example.
  • The line: <?php $total = $wp_query->post_count; ?> retrieve the number of total results.
  • The variable $current_page retrieve the current page of the results (more info).
  • The function can jump between the number of pages you only need to specify as third parameter.