Skip Navigation

[Resolved] Conditional on amount of repeatable fields

This thread is resolved. Here is a description of the problem and solution.

Problem:
Conditional on amount of repeatable fields

Solution:
There is no native way to count the repeating field's number of instances, you need to write a custom shortcode to get the repeating field instance count.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/conditional-on-amount-of-repeatable-fields/#post-1389441

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 4 years, 4 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by mattL-11 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1389329

We have a repeating date field which we'd like to use a conditional to show different content if there is more than 1 repeating field saved to the current post. Can you help, or point me to where I can see the conditional options for repeating fields?

#1389351

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to count the total repeating field entries and based on that total count you want to display some conditional content for the current post?

For instance, post X has 5 repeating date field entries and post Y has only 1, then you want to display something conditionally on post X. Where exactly you want to display conditional thing with your single post template?

#1389357

Hi,

That's right, I have a view for the repeating fields, which is rendered on the current post page. We just want to present the first date, with text after for any thing with >1 repeating fields.

#1389359

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you say you have a view for repeating fields - I think that should not be repeating fields but repeating field groups.

Can you please share screenshot of your edit view page so that I can confirm.

#1389383

It is just a repeating date field.

This is what we are currently using to render them all on the page.

[types field='departure-dates' style='text' format='j M, Y' separator=', ' item='$current_page'][/types]

#1389417

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so, finally, its repeating field.

You can use the attribute index to target your first instance.

For example:

[types field='departure-dates' style='text' format='j M, Y'  index="0"  item='$current_page'][/types]
 

=> https://toolset.com/documentation/user-guides/repeating-fields/#Index%20parameter%20Repeating%20Fields

Now, if you want to skip the first instance and display the other instances - you can use the [wpv-for-each] shortcode:

[wpv-for-each field="wpcf-departure-dates" start="2"]
[types field='departure-dates' style='text' format='j M, Y' item='$current_page'][/types]
[/wpv-for-each]

=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each

#1389437

Thanks, however, we wanted to render conditional text if there is greater than 1 repeating field.

[types field='departure-dates' style='text' format='j M, Y' index="0" item='$current_page'][/types]
This is fine to display the first field.

Then if more than 1 repeating field '...more dates available'.
Not the actual field data, is there a conditional to count the number of repeating fields?

#1389441

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok

1) Add the following code to your theme's functions.php or "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_shortcode('count_rf_entries', 'func_count_rf_entries');
function func_count_rf_entries($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
      
    $field = get_post_meta($atts['post_id'], "wpcf-".$atts['field'], false);
    $res = 0;
    if(is_array($field)){
        $res = count($field);
    }
    return $res;
}

2)
display the count result with above shortcodes, like this:

[count_rf_entries field="departure-dates"]

3)
You can display the conditional content using [wpv-conditional] shortcode:


Before using a custom shortcode inside a conditional, you need to register it. To do so, visit the Toolset -> Settings page and click the Front-end Content tab. There, simply add your shortcode to the Third-party shortcode arguments section.

For example;

[wpv-conditional if="( '[count_rf_entries field='departure-dates']' gt '1' )"]
 your conditional content goes here
[/wpv-conditional]

More info;
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes

#1389465

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.