Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * A simple shortcode to display the number of published podcasts on your site. Uses the `podcast` post type.
  5. *
  6. * Use the minus attribute to subtract the specified number from the final total.
  7. * Useful if you want to build a little suspense and create something like: We have 100+ podcasts!
  8. * (when in reality you have 101 podcasts)
  9. * [podcast-count minus="1"]
  10. *
  11. * Requires PHP 5.3+ for anonymous functions. Rewrite it if you want. You have been warned!
  12. */
  13.  
  14. add_action('init', function(){
  15.  
  16. add_shortcode('podcast-count', function( $atts ){
  17.  
  18. $attributes = shortcode_atts( array(
  19. 'minus' => 'false',
  20. 'add' => 'false'
  21. ), $atts );
  22.  
  23. // Store the query result. Change the post type slug if yours is different.
  24. $count = wp_count_posts( 'podcast' );
  25.  
  26. // get published podcasts
  27. $podcasts = isset( $count->publish ) ? $count->publish : 0;
  28.  
  29. // Subtract if necessary
  30. if ( $attributes['minus'] !== "false" ){
  31. $podcasts = $podcasts - intval($attributes['minus']);
  32. }
  33.  
  34. // Add if necessary
  35. if ( $attributes['add'] !== "false" ){
  36. $podcasts = $podcasts + intval($attributes['add']);
  37. }
  38.  
  39. return intval($podcasts);
  40.  
  41. });
  42.  
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement