[+] Display “1 of 6” type counter when iterating repeating fields using wpv-for-each

Register the following two custom shortcodes to be able to display counters such as “1 of 6” as you iterate through the instances of a custom field using the wpv-for-each shortcode:

add_shortcode('wpv-instances', function ($atts = []) {

    // provide defaults
    $atts = shortcode_atts(
        array(
            'field' => '',
        ),
        $atts
    );

    if ( isset( $atts['field'] ) ){
        global $post;
        $values = get_post_meta( $post->ID, $atts['field'], false );
        $count = count( $values );

        return $count;
    }

});

add_shortcode('wpv-index', function () {

    static $i = 1;
    $n = $i++;
    
    return $n;
});

They can then be used like so with the wpv-for-each shortcode:

[wpv-for-each field='wpcf-telefono']
<p>[types field='telefono'][/types] ([wpv-index] of [wpv-instances field='wpcf-telefono'])</p>
[/wpv-for-each]

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments