WordPress.org

Ready to get started?Download WordPress

Forums

Shortcode that counts each ocurrence of it in a post (3 posts)

  1. Andrés Sanhueza
    Member
    Posted 4 years ago #

    How can I make a shortcode that echoes the number of ocurrence in each post?

    Something like inserting:

    [foo /] [foo /] [foo /]

    And render:

    1 2 3

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    function foo_counter($atts) {    // [foo] keeps adding 1 everytime it is called in a post
    static $foo_count=0; $foo_count++; return $foo_count;
    }
    add_shortcode ('foo', 'foo_counter');

    use [foo] in the post

  3. Andrés Sanhueza
    Member
    Posted 4 years ago #

    That works, thanks.
    Is there a way to reset the count in each post (like when more than one appear in a loop)?

Topic Closed

This topic has been closed to new replies.

About this Topic