Last active
December 12, 2016 16:16
-
-
Save daronspence/11d276da9721994c7fb8dc6925f998c7 to your computer and use it in GitHub Desktop.
Podcast Count Shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Podcast Shortcode | |
* Description: Adds a shortcode to display your podcast count. <code>[podcast-count]</code> | |
* Version: 1.0 | |
* Author: Daron Spence | |
* Author URI: https://daronspence.com/ | |
* | |
* A simple shortcode to display the number of published podcasts on your site. Uses the `podcast` post type. | |
* | |
* Use the minus/plus attributes to change the specified number from the final total. | |
* Useful if you want to build a little suspense and create something like: "We have 100+ podcasts!" | |
* (when in reality you have 101 podcasts) | |
* | |
* [podcast-count minus="1"] or [podcast-count plus="3"] or [podcast-count minus="2" plus="2"] | |
* | |
* Requires PHP 5.3+ for anonymous functions. Rewrite it if you want. You have been warned! | |
*/ | |
add_action('init', function(){ | |
add_shortcode('podcast-count', function( $atts ){ | |
$attributes = shortcode_atts( array( | |
'minus' => 'false', | |
'plus' => 'false' | |
), $atts ); | |
// Store the query result. Change the post type slug if yours is different. | |
$count = wp_count_posts( 'podcast' ); | |
// get published podcasts | |
$podcasts = isset( $count->publish ) ? $count->publish : 0; | |
// Subtract if necessary | |
if ( $attributes['minus'] !== "false" ){ | |
$podcasts = $podcasts - intval($attributes['minus']); | |
} | |
// Add if necessary | |
if ( $attributes['plus'] !== "false" ){ | |
$podcasts = $podcasts + intval($attributes['plus']); | |
} | |
return intval($podcasts); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment