Advertisement
pusatdata

Add DESC Number Before Title Post

Apr 23rd, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. functions.php:
  2. ===============
  3. /*
  4. ---------------------
  5. Nomor DESC
  6. ---------------------
  7. */
  8.  
  9. function get_post_position_in_reverse()
  10. {
  11. // Invoke the global $wp_query object
  12. global $wp_query;
  13.  
  14. // Make sure that we are actually inside the loop, if not, bail
  15. if ( !in_the_loop() )
  16. return false;
  17.  
  18. //Setup our variables we will be using
  19. // Get the current page we are on
  20. if ( get_query_var( 'paged' ) ) {
  21. $current_page = get_query_var( 'paged' );
  22. } elseif ( get_query_var( 'page' ) ) {
  23. $current_page = get_query_var( 'page' );
  24. } else {
  25. $current_page = 1;
  26. }
  27.  
  28. // Get the current post's position plus 1 as post counter starts at 0
  29. $post_position = $wp_query->current_post + 1;
  30. // Get the total amount of posts in the query
  31. $total_posts = $wp_query->found_posts;
  32. // Get the amount of posts_per_page from backend
  33. $ppp = get_option( 'posts_per_page' );
  34.  
  35. /**
  36. * Now that we have everything set up, we need to do the maths
  37. *
  38. * If we have 20 posts across 4 pages with 6 posts per page, we will have
  39. * 6 posts on pages 1,2 and 3 and only 2 posts on page 4. Our post numbers
  40. * will be as follow, the first post on page one will be 20 and the last
  41. * post on page 4 will be 1
  42. */
  43. $number = $total_posts - ( ( $current_page * $ppp ) - ( $ppp - $post_position ) ) + 1;
  44.  
  45. return number_format_i18n( $number );
  46. }
  47.  
  48. index.php loop:
  49. ---------------
  50. <?php echo get_post_position_in_reverse(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement