pusatdata

WP Trik: Add DESC Number Before Title

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