How to check if this is the nth instance of a given shortcode in a post

If you’re using a class, it would be

class MyShortcode {

static $instance = 0;

    function __construct($args = array()) {
      add_action( 'init', array(&$this, 'init') );
    }

    function init() {
      add_shortcode('myshortcode', array(&$this, 'shortcode'));
    }

    static function shortcode($atts) {
      //this is the code of your shortcode

      // you can increment your counter
      self::$instance++;

    }

}

Leave a Comment