Last active
June 22, 2024 22:04
-
-
Save LaxusCroco/9b4ccec0af5697331c0eb19f23d6e964 to your computer and use it in GitHub Desktop.
Adds a callback for Dynamic Field "Get value from repeater field". It allows getting the value from the specified sub-field and row number of the repeater on the single post page and in listings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'jet-engine/callbacks/register', function( $callbacks ) { | |
$args = array( | |
'repeater_name' => array( | |
'label' => 'Repeater Field key', | |
'type' => 'text', | |
), | |
'sub_field_name' => array( | |
'label' => 'Sub-field name', | |
'type' => 'text', | |
), | |
'row_number' => array( | |
'label' => 'Row number starting from 0', | |
'type' => 'text', | |
), | |
); | |
$callbacks->register_callback( 'jec_repeater_callback', 'Get value from repeater field', $args ); | |
function jec_repeater_callback( $value, $rep_name, $sub_field_name, $row ) { | |
if ( empty( $value ) ) { | |
return; | |
} | |
$result = ''; | |
if ( empty( $rep_name ) || empty( $sub_field_name ) ) { | |
return ''; | |
} | |
$index = 0; | |
if ( is_numeric( $row ) ) { | |
if ( str_starts_with( $row, '+' ) ) { | |
$coff = trim( $row, '+' ); | |
$row = $index + + $coff; | |
} | |
if ( str_starts_with( $row, '-' ) ) { | |
$coff = trim( $row, '-' ); | |
$row = $index - + $coff; | |
} | |
} else { | |
$row = $index; | |
} | |
$fields = get_post_meta( $value, $rep_name, true ); | |
$row = $fields[ 'item-' . $row ] ?? array(); | |
return $row[ $sub_field_name ] ?? ''; | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment