Skip Navigation

[Resolved] How to make custom function more flexible?

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

Problem:

How to use a custom shortcode [get_rfg_count]

Solution:

You can use above shortcode for another post type relationship, for example:

[get_rfg_count type="my-relationship-B"]

Relevant Documentation:

This support ticket is created 3 years, 7 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by gordonW-2 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2054701

Hello.

In my dataset I have a number of repeater groups, i.e. product colors, product shapes, etc ...

it is important that I am able to get the number of items in those groups so that I can tweak the display of the content-template appropriately.

Currently, I am using this function that was posted in response to another developer's question:

function func_get_repeating_field_group_count( $atts ) {
    $a = shortcode_atts( array(
        'id' => get_the_ID(),
        'type' => 'product-colorway',
    ), $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' );

and then I trigger it by utilizing the new shortcode like this (to show content if the count is ≥ 4):

  ( ( '[get_rfg_count]' gte '4' ) ) 

my question is this: how do I modify this function so that I can use it on different fields, instead of the hard-coded "product-colorway" as I have it shown above?

again ... I would like to use a conditional block that will look at the COUNT() of a particular repeater and
if it has a COUNT() between 0 and 4 show VIEW A
if it has a COUNT() ≥ 4 then show VIEW B

thx,
Gordon

#2054985

Hello,

You can use above shortcode for another post type relationship, for example:
[get_rfg_count type="my-relationship-B"]

Please replace my-relationship-B with your other post type relationship slugs.

#2055483

Excellent.... that was exactly what I needed to know.

My issue is resolved now. Thank you!