Agregamos el código PHP en function.php:
class Jet_Engine_Repeater_Shortcode {
public function __construct() {
add_shortcode( 'jet_repeater', [ $this, 'get_content' ] );
}
public function get_post_id() {
if ( function_exists( 'jet_engine' ) ) {
return jet_engine()->listings->data->get_current_object_id();
}
return get_the_ID();
}
public function get_row_index( $row ) {
$index = 0;
if ( function_exists( 'jet_engine' ) ) {
$index = jet_engine()->listings->data->get_index();
}
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;
}
return $row;
}
return $index;
}
public function get_content( $attrs = array() ) {
$attrs = shortcode_atts( array(
'id' => '',
'repeater_name' => '',
'row' => '',
'sub_field_name' => '',
'svg' => '',
), $attrs, 'jet_repeater' );
if ( empty( $attrs['repeater_name'] ) && empty( $attrs['sub_field_name'] ) ) {
return 0;
}
if ( empty( $attrs['id'] ) ) {
$attrs['id'] = $this->get_post_id();
}
$attrs['row'] = $this->get_row_index( $attrs['row'] );
$fields = get_post_meta( $attrs['id'], $attrs['repeater_name'], true );
$row = $fields[ 'item-' . $attrs['row'] ] ?? array();
if ( ! empty($attrs['svg']) && $row[ $attrs['sub_field_name'] ] ) {
$attachment = wp_get_attachment_image($row[ $attrs['sub_field_name']]);
return $attachment;
}
return $row[ $attrs['sub_field_name'] ] ?? 0;
}
}
new Jet_Engine_Repeater_Shortcode();