Skip Navigation

[Resolved] Hide/show content based on if repeatable field (or child post) has been added

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

Problem:
How to conditionally display content depending on whether that content has any repeatable field groups (or child posts).

Solution:
This requires registering a custom shortcode which returns the number of child posts (or repeatable field groups) a post has.

The code is as follows:

/**
 * Register connections shortcode
 *
 * <a href="https://toolset.com/forums/users/att/" tabindex="80">@att</a> (string) relationship : post relationship slug
 * @return count of connected posts
 */
add_shortcode( 'connections', function( $atts = [] ){
 
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'relationship'      =>   '',
        ), 
        $atts
    );
 
    global $post;
    $count = 0;
 
    $relationship = toolset_get_relationship( $atts['relationship'] );
 
    if ( $relationship ) {
 
        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;
 
        $origin = ( $parent == $type ) ? 'parent' : 'child';
 
        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], $origin, 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );
 
    }
 
    return $count;
});

To use it inside a conditional shortcode requires registering the custom shortcode at Toolset > Settings > Front-end Content.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
https://toolset.com/documentation/adding-custom-code/

This support ticket is created 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Dallin Chase 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1109698
Screen Shot 2018-09-17 at 2.17.13 PM.png

Hey guys,

I am trying to hide/show content depending on wether or not custom repeater fields have been added or not. Essentially, if I have added repeater fields to the custom post type, I'd like to show them. If not, I'd like to hide the div.

Let me know if you need more info. It's a little bit hard to explain.

#1110354

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I wrote a custom shortcode recently which, for post relationships, returns a count of the number of connected posts (for the relationship you specify as an attribute).

Under the hood repeatable field groups are essentially implemented as post relationships and use the same API, so the shortcode should work in this case, too.

Add the following code to your site. If you update to Types 3.1 and go to Toolset > Settings you'll see a new tab where you can add code snippets without having to edit your theme's functions.php.

/**
 * Register connections shortcode
 *
 * @att (string) relationship : post relationship slug
 * @return count of connected posts
 */
add_shortcode( 'connections', function( $atts = [] ){

	// provide defaults
	$atts = shortcode_atts( 
		array(
			'relationship'		=>	'',
		), 
		$atts 
	);

	global $post;
	$count = 0;

	$relationship = toolset_get_relationship( $atts['relationship'] );

	if ( $relationship ) {

		$parent = $relationship['roles']['parent']['types'][0];
		$child = $relationship['roles']['child']['types'][0];
		$type = $post->post_type;

		$origin = ( $parent == $type ) ? 'parent' : 'child';

		// Get connected posts
		$connections = toolset_get_related_posts( $post->ID, $atts['relationship'], $origin, 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );

	}

	return $count;
});

You would use it like so:

<p>number of Issue repeatable field groups for the current post: [connections relationship="issue"]</p>

In this example, my repeatable field group has a slug of "issue", so that is the slug of the relationship which is passed to the shortcode.

To be able to use this in conditions you need to go to Toolset > Settings > Front-end content and register the custom shortcode.

#1110708

Thank you Nigel! This worked like a charm!

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