Skip Navigation

[Resolved] contidional with repeating field groups

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

Problem:
conditional with repeating field groups

Solution:
To count the number of repeating field group entries. You will require custom shortcode that will return the number of count.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/contidional-with-repeating-field-groups/#post-1779671

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 3 years, 6 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 5 replies, has 2 voices.

Last updated by BrandenT2154 3 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1777945

I have the following bit of code on a singular content template

[wpv-conditional if="( $(wpcf-3d-model) ne '' ) OR ( $(wpcf-full_2d) ne '' )"]<h3>Downloadble Information</h3>[/wpv-conditional]
[wpv-conditional if="( $(wpcf-3d-model) ne '' )"]<i class="fas fa-cube"> 3D Model File: [types field='3d-model' title='Click Here To Download'][/types][/wpv-conditional]
[wpv-conditional if="( $(wpcf-full_2d) ne '' )"]<i class="far fa-file-alt"> 2D Drawing File: [types field='full_2d' title='Click Here To Download'][/types][/wpv-conditional]
[wpv-view name="tech-data"]

I have 4 things here

1. a header
2. a link to a 3d model
3. a link to a 2d drawing
4. a toolset view which displays a loop of a repeating field group consisting of a text field (name) and a file, which is dispalyed as a link.

I only want to display the header if one of these conditions is true: there is a 3d model, OR there is a 2d model, OR there is an item in the tech-data loop.

how would i go about constructing a condition for that?

If you need to see the admin, CLICK HERE TO LOG IN AUTOMATICALLY: hidden link

here's the view hidden link

here's the content template: hidden link

#1778227

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share little more information, when you say "header", when is the header part you referring to. Can you please share screenshot of the header that you want to display conditionally.

#1779207
Capture.PNG

hello, you can see it here: hidden link

a screenshot is attached. the header is just the h3 tag you see in the code. it reads <h3>downloadable information</h3>

if there is no data in any of the custom fields circled in red i want to hide everything circled in red. the problem is that one is a repeating field group and there doesnt appear to be a way to make a conditional for that.

#1779671

Minesh
Supporter

Languages: English (English )

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

There are multiple ways we can achieve this.

We can either use JS code or use the Toolset post-relationship API function to get the total number of count of repeating field. In your case we will try to get the total number of count of your repeating field group Tech data aka "tech-data-air-filter".

To count the number of repeating field group entries. You will require custom shortcode that will return the number of count.

You should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
Or
You can add it to your current theme's functions.php file:

function func_get_repeating_field_group_count( $atts ) {
    $a = shortcode_atts( array(
        'id' => get_the_ID(),
        'type' => 'tech-data-air-filter',
    ), $atts );
 
    if ( (!empty($a['id'])) && (!empty($a['type'])) )
    {
        $section_ids = toolset_get_related_posts(
            $a['id'],
            $a['type'],
            'parent',
            1000,
            0,
            array(),
            'post_id',
            'child'
        );
         
        return count($section_ids);
    }
 
}
add_shortcode( 'get_rfg_count', 'func_get_repeating_field_group_count' );

Before using a custom shortcode [get_rfg_count] 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.
- I already registered it for you.

Now, you just simply require to modify your conditional statement as given under:

[wpv-conditional if="( $(wpcf-3d-model) ne '' ) OR ( $(wpcf-full_2d) ne '' ) OR ( '[get_rfg_count]' gt '0' )"]
<h3>Downloadble Information</h3>
[/wpv-conditional]

More info:
=> https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Can you please try and check if the solution I shared help you to resolve your issue.

#1783139

Hi, this doesn't seem to work.

I didn't see the function registered like you mentioned so i added it to functions.php

I added the shortcode into the conditional like you mentioned here: hidden link

i've also just added the short code by itself to see if it's counting. as you can see, it doesn't seem to be counting or doing anything, since the shortcode itself is visible on the front end.

any ideas?

#1783141

ah nevermind, wrong theme file. its fixed now

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