Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Podcast Count Shortcode
<?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
You can’t perform that action at this time.