Hello.
I have a repeater field for "Installation Image" called "installation-image"
If there is only 1 image in that field array I would like to display it using the image block.
If there are more than 1 image, I would like to display the images using the slider block.
Since the conditional field block wont let you determine the number of items in a block, how can you do this?
I tried following the instructions here: https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/#check-if-a-types-repeating-field-has-a-n-th-item
but whenever I paste the code here:
https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/#check-if-a-types-repeating-field-has-a-n-th-item
into my functions.php file.
wordpress gives me a critical wordpress error.
that there is an unexpected ";" in this line: if ( isset( $wpcf ) ...
Secondly, in the content template is it as simple as doing this:
[wpv-conditional if="( prefix_check_repeating_field_index_exists('installation-image',1) eq 0 )"]
<IMAGE BLOCK>
[/wpv-conditional]
[wpv-conditional if="( prefix_check_repeating_field_index_exists('installation-image',1) eq 1 )"]
<SLIDER BLOCK>
[/wpv-conditional]
or is there an "else" version of the wpv-conditional shortcode?
thx
Gordon
Hi, it looks like there was a problem when this code snippet was inserted in the documentation. Several symbols were converted to their HTML equivalents. The correct code snippet is:
function prefix_check_repeating_field_index_exists($field_id, $index, $mode, $object) {
global $wpcf;
if ( isset( $wpcf ) && function_exists('types_get_field') && $mode == 'posts' ) {
$field = types_get_field( $field_id );
if ( types_is_repetitive( $field ) ) {
$wpcf->repeater->set( $object->ID, $field );
$_meta = $wpcf->repeater->_get_meta();
$meta = $_meta['custom_order'];
if ( count( $meta ) > $index ) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
} else {
return 0;
}
}
I've asked our documentation team to update the code to fix this entity conversion issue. Sorry for the inconvenience this caused.
Secondly, in the content template is it as simple as doing this:...or is there an "else" version of the wpv-conditional shortcode?
Correct, you need two "if" conditions here because there is no "else" conditional clause. Looks good to me, let me know if it still isn't working as expected after updating your PHP code as shown above.
ok thx!
the function now works without errors ... was about the only thing I didn't try. ¯\_(ツ)_/¯
anyway, what is the best way to get this:
[wpv-conditional if="( prefix_check_repeating_field_index_exists('installation-image',1) eq 0 )"]
<IMAGE BLOCK>
[/wpv-conditional]
[wpv-conditional if="( prefix_check_repeating_field_index_exists('installation-image',1) eq 1 )"]
<SLIDER BLOCK>
[/wpv-conditional]
into the page? do I use the conditional block > advanced formatting? or the Fields & Text block? or something else? thx!
do I use the conditional block > advanced formatting?
Yes, if you want to display an image block or a slider block conditionally, it is best to use a conditional block in the block editor. You should turn on the advanced conditions editor and add your conditional code in the advanced editor interface like so:
( ( prefix_check_repeating_field_index_exists('installation-image',1) eq 0 ) )
or
( ( prefix_check_repeating_field_index_exists('installation-image',1) eq 1 ) )
Be sure to register the prefix_check_repeating_field_index_exists function in Toolset > Settings > Front-end Content > "Functions inside conditional evaluations", as explained in the documentation.
( ( prefix_check_repeating_field_index_exists('installation-shot',1) eq '0' ) )
so this function checks the "installation-shot" array to see if there is an index 1.
If there is no index 1 then return a "0"
If the returned value == 0 then display the block of code.
this returns:
( ( prefix_check_repeating_field_index_exists(‘installation-shot’,1) eq ‘0’ ) )
then I echo the values for 'installation-shot' as raw data
but this shouldn't happen tho, right? the fact that two URLs are returned should indicate that the conditional fails, but yet it is shown...
What you don't see is that I have another conditional block right below that is set up like:
( ( prefix_check_repeating_field_index_exists('installation-shot',1) eq '1' ) )
this should return:
( ( prefix_check_repeating_field_index_exists(‘installation-shot’,1) eq ‘1’ ) )
then the values for 'installation-shot' are echoed back as raw data
But this doesn't trigger. so what am I doing wrong?
here is the function that I am using:
function prefix_check_repeating_field_index_exists($field_id, $index, $mode, $object) {
global $wpcf;
if ( isset( $wpcf ) && function_exists('types_get_field') && $mode == 'posts' ) {
$field = types_get_field( $field_id );
if ( types_is_repetitive( $field ) ) {
$wpcf->repeater->set( $object->ID, $field );
$_meta = $wpcf->repeater->_get_meta();
$meta = $_meta['custom_order'];
if ( count( $meta ) > $index ) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
} else {
return 0;
}
}
and it is registered
not sure what the problem was that I was having, but after the 32nd try, it started working.
c'est la vie
My issue is resolved now. Thank you!
Okay that is odd, but glad to know it seems to be resolved...for now. Feel free to reopen this ticket or open a new ticket if the problem returns.