Skip Navigation

[Resolved] count the items in an RFG inside an RFG and use it in condition

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

Problem:
The user would like to display a different icon based on the count of nested RFG items in a nested view.

Solution:
Toolset does not offer built-in feature to count the number of items in an RFG. It will require custom code.

However, based on how the user has set up the nested view, we can implement a solution using Javascript. Check this reply https://toolset.com/forums/topic/count-the-items-in-an-rfg-inside-an-rfg-and-use-it-in-condition/#post-2041359

This support ticket is created 2 years, 11 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by jorgel-4 2 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2040843

I need to count the number of items in an RFG inside an RFG and use the count inside a condition

#2040887
#2041359

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I could not make the custom code return the count of the images in each unit, either using the view, or using the relationship API(toolset_get_related_posts). However, I was able to come up with a Javascript-based solution that is optimal.

Instead of counting the number of images using a shortcode or a function in the server, which will trigger more database queries and increase the page load, I added a special class(no-unit-image) to the wpv-no-items-found section of the view:

	[wpv-no-items-found]
		[wpml-string context="wpv-views"]<small class="no-unit-images"></small>[/wpml-string]
	[/wpv-no-items-found]

Then using a Javascript, I search for this class, which means the unit does not have any images, and then, I apply the greyscale style for the image button.

jQuery(function($){
    $('.no-unit-images').each(function(){
        var $this = jQuery(this)
        var container = $this.closest('.ml-0');
        var image = container.find('.btn-unit-img>img');
        image.attr('style', 'webkit-filter: grayscale(100%); filter: grayscale(100%);')
    })
})

Please check if that's what you are looking for hidden link

#2041397

Awesome!! Thank a lot Jamal, it's working perfectly now!! 😀

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