Hello!
Yes, you can absolutely achieve this dynamically using a custom PHP function hooked in WordPress that outputs inline CSS or modifies the section's style attribute directly, targeting each "it_staff_section" individually with its own background image from your repeater field.
Problem Breakdown:
- You have a repeater meta field
it-staff-repeater containing multiple items.
- Each repeater item has
it_staff_image and it_staff_title.
- You are using a Listing Item in Elementor to output each repeater item as a section with a class "it_staff_section".
- Elementor doesn't provide a dynamic option to assign the
it_staff_image media as a background image on the section.
- You want a solution where for each
it_staff_section the background image is set according to the repeater item image.
Proposed Solution
1. Print Each Section with a Unique Identifier
You need to have a unique ID or index on each repeater item wrapper/section so you can target it individually with CSS or inline styles.
Example:
<div class="it_staff_section" data-item-index="0">...</div>
<div class="it_staff_section" data-item-index="1">...</div>
You can output this either by:
- Adjusting your Listing Item template in JetEngine to add an index or unique class,
- Or adding an attribute dynamically.
If the Listing Item cannot do this, a workaround is to hook on jet-engine/listing/dynamic-field-args or similar hooks JetEngine provides, or use JavaScript — but ideally do it in PHP.
2. Write a PHP function that loads the repeater fields for the current post, and then outputs CSS targeting each item by index or unique class, setting the CSS background-image property with the URL of it_staff_image dynamically.
3. Add function to enqueue inline styles for the background images (or output inline styles in the page head).
Step-by-Step Example
Step 1: Add a unique index data attribute to each repeater item output
I assume you control the Listing Item template in JetEngine — inside the Listing Item, maybe you can add a custom HTML or dynamic field that outputs the current index.
If not possible, you can do this by adding a filter in PHP inside functions.php. For example:
add_filter( 'jet-engine/listing/allowed-html-tags', function( $tags ) {
// Allow data-* attributes in HTML of listing item wrappers if needed.
return $tags;
});
Alternatively, if you cannot add a data attribute via JetEngine easily, you can select those sections with JavaScript and assign indexes, but PHP is preferable.
Step 2: PHP to output CSS dynamically for background images
Add this to your child theme’s functions.php or via code snippet plugin:
function add_it_staff_section_background_styles() {
if ( ! is_singular() ) {
return;
}
$post_id = get_the_ID();
// Get repeater data
$it_staff_repeater = jet_engine()->listings->data->get_meta($post_id, 'it-staff-repeater');
if ( empty( $it_staff_repeater ) || ! is_array( $it_staff_repeater ) ) {
return;
}
// CSS string
$css = '';
foreach ( $it_staff_repeater as $index => $item ) {
// Get the image ID from repeater item
$image_id = isset( $item['it_staff_image'] ) ? $item['it_staff_image'] : '';
if ( ! $image_id ) {
continue;
}
// Get the image URL (use 'full' size or any desired)
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
if ( ! $image_url ) {
continue;
}
// Target each container by index - assuming .it_staff_section[data-item-index="0"] in HTML
$css .= "
.it_staff_section[data-item-index='{$index}'] {
background-image: url('{$image_url}');
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
}
";
}
if ( ! empty( $css ) ) {
wp_add_inline_style( 'elementor-frontend', $css );
}
}
add_action( 'wp_enqueue_scripts', 'add_it_staff_section_background_styles', 20 );
Important Notes:
- You must have the attribute
data-item-index on each .it_staff_section in the HTML output of your listing item. This is mandatory to target the correct background image per section.
- Make sure the repeater meta key matches and is accessible in the current post.
- Optionally, you can add fallback styles or logic if no image is set.
- Inline styles are hooked on
elementor-frontend style handle, so it loads after Elementor styles.
- You can customize selectors as required.
Step 3: Add the data-item-index attribute to each repeater item wrapper in the Listing Item
If JetEngine allows inserting listing item index via shortcode or dynamic tag:
Try adding a HTML widget inside Listing Item with:
<div class="it_staff_section" data-item-index="%listing_current_item%" >
<!-- your fields here -->
</div>
%listing_current_item% is a JetEngine dynamic macro for the current repeater item index — verify what is the exact macro, or use rel="listing_item_index", or consult JetEngine docs.
If this is not possible, you can add the data attribute using JavaScript like this jQuery snippet (enqueue it in Elementor template page):
jQuery(document).ready(function($){
$('.it_staff_section').each(function(index){
$(this).attr('data-item-index', index);
});
});
This is a fallback if no native macro exists in JetEngine for the index.
Alternative: Add inline style attribute directly from PHP
If you can modify the repeating section's wrapper HTML directly, add the background image as inline style:
$style = 'background-image: url(' . esc_url($image_url) . '); background-size: cover; background-position: center; background-repeat: no-repeat;';
echo '<div class="it_staff_section" style="'. esc_attr( $style ) .'">';
But this depends on whether you can customize the Listing Item PHP or template.
Summary
- Assign a unique index or ID on each
.it_staff_section element.
- Use a PHP function hooking into
wp_enqueue_scripts to collect the repeater images.
- Output inline CSS targeting each
.it_staff_section[data-item-index="{index}"] with the correct background-image.
- Alternatively, add inline styles directly if you can customize the HTML template that outputs the repeater item.
Let me know if you want help with the exact way to add the index attribute inside JetEngine listing item template, or prefer a JavaScript-only approach!