Skip Navigation

[Gelöst] How to retrieve the number of items in a multi occurrence field

This support ticket is created vor 5 Jahren, 4 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Pat vor 5 Jahren, 4 Monaten.

Assistiert von: Luo Yang.

Author
Artikel
#1281547

Pat

Hello,

I have created a date field with possibility to have multiple values.

Now, I would like to retrieve the number of values that this field have (in a specific post). In fact, I want to construct a table with the list of the dates in columns. So, I need to define how many columns this table should have.

Thanks for your help
Regards
Pat

#1281645

Dear Pat,

There isn't such a built-in feature within Views plugin, however, you can create a custom shortcode to get the count of multiple instance field, for example:

There is a multiple instance date field "my-date-field" created with Types plugin, you can try these:

1) Add below codes into your theme/functions.php:

add_shortcode('count_instances', 'count_instances_func');
function count_instances_func($atts, $content){
    $atts = shortcode_atts( array(
        'field' => 0,
        'post_id' => get_the_ID(),
    ), $atts );
     
    $field = get_post_meta($atts['post_id'], $atts['field'], false);
    $res = 0;
    if(is_array($field)){
        $res = count($field);
    }
    return $res;
}

2) display the count result with above shortcode, like this:
[count_instances field="wpcf-my-date-field"]

#1281743

Pat

Hi Luo,

That's working fine. Now, I would like to built the table part and need to return something like : </td><td></td><td></td><td>
The number of </td><td> sequences depending of the nb of your function.

I have tried but it seems that the </td><td> is not rendered correctly.
Any idea?
Regards
Pat

Neue Threads, die von Luo Yang erstellt wurden und mit diesem verbunden sind, sind unten aufgeführt:

https://toolset.com/de/forums/topic/build-the-table-part-on-multiple-instances-field/

#1282381

I assume the original question of this thread is resolved, for the new question, please check the new thread here:
https://toolset.com/forums/topic/build-the-table-part-on-multiple-instances-field/

#1282695

Pat

Thanks Luo,

Works perfectly!
Regards
Pat