Skip to content

Instantly share code, notes, and snippets.

@lidocaine
Created October 30, 2014 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidocaine/cbc9942c52857fe28229 to your computer and use it in GitHub Desktop.
Save lidocaine/cbc9942c52857fe28229 to your computer and use it in GitHub Desktop.
Useful Wordpress snippets I've had to come up with and will probably need to use again.
/*
*
* Getting appropriate "X-Y of Z results" in searches
* with appropriate post numbering across pages
*
*/
// Setting up variables for math to display
// proper search result counts across all pages...
$posts_per_page = get_option('posts_per_page');
$page_num = (get_query_var('paged')) ? get_query_var('paged') - 1 : 0;
$offset = ($page_num) * $posts_per_page;
$page_total = $offset + $wp_query->post_count;
echo '<span class="search-result-count">'.($offset + 1).'-'.$page_total.' <em>of</em> .'$wp_query->found_posts.' results</span>';
// To display the proper search result number we gotta do some more math...
// Must be used within The Loop to access $current_post
$real_index = ($wp_query->current_post + 1) + ( $posts_per_page * ($page_num) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment