{"id":5560,"date":"2023-12-28T04:53:14","date_gmt":"2023-12-28T04:53:14","guid":{"rendered":"https:\/\/graphica.pe\/repeater-field-on-listing\/"},"modified":"2024-07-11T20:55:44","modified_gmt":"2024-07-11T20:55:44","slug":"repeater-field-on-listing","status":"publish","type":"post","link":"https:\/\/graphica.pe\/en\/repeater-field-on-listing\/","title":{"rendered":"Repeater field on Listing"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"5560\" class=\"elementor elementor-5560 elementor-4617\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-a626c5b e-flex e-con-boxed e-con e-parent\" data-id=\"a626c5b\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-341bb54 elementor-widget elementor-widget-text-editor\" data-id=\"341bb54\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We add the PHP code in function.php:<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-72d01a0b elementor-widget elementor-widget-code-highlight\" data-id=\"72d01a0b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-twilight copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-php line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-php\">\n\t\t\t\t\t<xmp>class Jet_Engine_Repeater_Shortcode {\r\n\r\n\tpublic function __construct() {\r\n\t\tadd_shortcode( 'jet_repeater', [ $this, 'get_content' ] );\r\n\t}\r\n\r\n\tpublic function get_post_id() {\r\n\r\n\t\tif ( function_exists( 'jet_engine' ) ) {\r\n\t\t\treturn jet_engine()->listings->data->get_current_object_id();\r\n\t\t}\r\n\r\n\t\treturn get_the_ID();\r\n\t}\r\n\r\n\tpublic function get_row_index( $row ) {\r\n\t\t$index = 0;\r\n\r\n\t\tif ( function_exists( 'jet_engine' ) ) {\r\n\t\t\t$index = jet_engine()->listings->data->get_index();\r\n\t\t}\r\n\r\n\t\tif ( is_numeric( $row ) ) {\r\n\r\n\t\t\tif ( str_starts_with( $row, '+' ) ) {\r\n\t\t\t\t$coff  = trim( $row, '+' );\r\n\t\t\t\t$row = $index + + $coff;\r\n\t\t\t}\r\n\r\n\t\t\tif ( str_starts_with( $row, '-' ) ) {\r\n\t\t\t\t$coff  = trim( $row, '-' );\r\n\t\t\t\t$row = $index - + $coff;\r\n\t\t\t}\r\n\r\n\t\t\treturn $row;\r\n\r\n\t\t}\r\n\r\n\t\treturn $index;\r\n\t}\r\n\r\n\tpublic function get_content( $attrs = array() ) {\r\n\r\n\t\t$attrs = shortcode_atts( array(\r\n\t\t\t'id'             => '',\r\n\t\t\t'repeater_name'  => '',\r\n\t\t\t'row'            => '',\r\n\t\t\t'sub_field_name' => '',\r\n\t\t\t'svg' => '',\r\n\t\t), $attrs, 'jet_repeater' );\r\n\r\n\t\tif ( empty( $attrs['repeater_name'] ) && empty( $attrs['sub_field_name'] ) ) {\r\n\t\t\treturn 0;\r\n\t\t}\r\n\r\n\t\tif ( empty( $attrs['id'] ) ) {\r\n\t\t\t$attrs['id'] = $this->get_post_id();\r\n\t\t}\r\n\r\n\t\t$attrs['row'] = $this->get_row_index( $attrs['row'] );\r\n\r\n\t\t$fields = get_post_meta( $attrs['id'], $attrs['repeater_name'], true );\r\n\t\t$row    = $fields[ 'item-' . $attrs['row'] ] ?? array();\r\n\t\t\r\n\t\tif ( ! empty($attrs['svg']) && $row[ $attrs['sub_field_name'] ] ) {\r\n\t\t\t$attachment = wp_get_attachment_image($row[ $attrs['sub_field_name']]);\r\n\r\n\t\t\treturn $attachment;\r\n\t\t}\r\n\r\n\t\treturn $row[ $attrs['sub_field_name'] ] ?? 0;\r\n\t}\r\n}\r\n\r\nnew Jet_Engine_Repeater_Shortcode();<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>We add the PHP code in function.php: class Jet_Engine_Repeater_Shortcode { public function __construct() { add_shortcode( &#8216;jet_repeater&#8217;, [ $this, &#8216;get_content&#8217; ] ); } public function get_post_id() { if ( function_exists( &#8216;jet_engine&#8217; ) ) { return jet_engine()->listings->data->get_current_object_id(); } return get_the_ID(); } public function get_row_index( $row ) { $index = 0; if ( function_exists( &#8216;jet_engine&#8217; ) ) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-5560","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/posts\/5560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/comments?post=5560"}],"version-history":[{"count":0,"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/posts\/5560\/revisions"}],"wp:attachment":[{"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/media?parent=5560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/categories?post=5560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/graphica.pe\/en\/wp-json\/wp\/v2\/tags?post=5560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}