Hello! So in WordPress, I have created custom Meta Boxes with JetEngine plugin for IT…
- User
Hello! So in WordPress, I have created custom Meta Boxes with JetEngine plugin for IT Staff with a repeater field name called "it-staff-repeater", which has two fields inside: it_staff_image (media) and it_staff_title (text). Using a Listing Item, and Elementor page builder, I wanted to set up the "it_staff_image" as a background for the section, but it looks like there is no option to insert it. Is it possible to set it up dynamically using a custom function? If it's needed to assign a custom class to the section like "it_staff_section", there is no problem, I can set it up, but please help me with the function or something like that.
- Assistant
Hello!
Yes, you can definitely achieve this by writing a custom function in your theme's
functions.php(preferably a child theme), or via a functionality plugin, that outputs the needed CSS inline or as a<style>block targeting your section with classit_staff_section. This function will fetch the repeater meta fields using JetEngine's functions, get the image URL for the first item in the repeater, and output CSS to set that image as the background for.it_staff_section.
What you want:
- For the Elementor section that has class
.it_staff_section - Dynamically set the background image using the first
it_staff_imagein the repeaterit-staff-repeater
Step-by-step solution:
1. Add the custom class to your Elementor section
In Elementor, select your section, go to Advanced > CSS Classes and add:
it_staff_section
2. Add a function to output inline CSS with the background image
Add this snippet to your child theme's
functions.phpor a site-specific plugin:add_action( 'wp_head', 'add_it_staff_section_background' ); function add_it_staff_section_background() { if ( ! is_singular() ) { return; // Only run on single posts/pages where meta is expected } global $post; // Using JetEngine function to get repeater meta // JetEngine stores repeater fields as serialized arrays in post meta, but it also provides helper functions. // Unfortunately, JetEngine does not have global helper functions documented, but the meta is stored in post meta. // Get repeater data (it-staff-repeater) $repeater_data = get_post_meta( $post->ID, 'it-staff-repeater', true ); if ( ! empty( $repeater_data ) && is_array( $repeater_data ) ) { // Check first repeater item $first_item = reset( $repeater_data ); if ( isset( $first_item['it_staff_image'] ) && ! empty( $first_item['it_staff_image'] ) ) { // Could be an ID or an array depending on JetEngine settings $image = $first_item['it_staff_image']; // The image could be stored as an attachment ID or as URL if ( is_numeric( $image ) ) { // Get image URL from ID $image_url = wp_get_attachment_image_url( $image, 'full' ); } elseif ( is_array( $image ) && isset( $image['url'] ) ) { // If image stored as array $image_url = $image['url']; } else { // Assume it's URL string $image_url = esc_url( $image ); } if ( $image_url ) { ?> <style> .it_staff_section { background-image: url('<?php echo esc_url( $image_url ); ?>'); background-size: cover; background-position: center center; background-repeat: no-repeat; } </style> <?php } } } }
Explanation:
- Hooks into
wp_headto output a<style>block - Gets the current post's
it-staff-repeatermeta, grabs the first row'sit_staff_image - Converts the image ID to URL (or uses direct URL if needed)
- Injects CSS targeting
.it_staff_sectionto set the background image and common background styles
3. Additional tips:
- Make sure your repeater meta exists on the current post and has data.
- You can adjust the code to pick a different repeater index or loop through all images if you want more complex behavior.
- If the repeater is not saved as an array, try inspecting the post meta in the database or use
get_post_meta($post->ID)and dump it to see the exact structure.
If you'd like, I can help you expand this to handle multiple images or different fields inside the repeater!
Let me know how it goes!
- For the Elementor section that has class
- User
Does this work if the media file is set to media ID instead of URL?
- Assistant
Yes, the snippet I gave already handles media stored as an attachment ID correctly.
Here’s the relevant part:
if ( is_numeric( $image ) ) { // Get image URL from ID $image_url = wp_get_attachment_image_url( $image, 'full' ); }JetEngine usually stores media fields as the attachment ID (an integer) by default. So when your
it_staff_imageis a media ID, this code will detect it as numeric, and usewp_get_attachment_image_url()to get the full URL of the image.
Summary
- If the stored value in
it_staff_imageis numeric (attachment ID), the code uses the WordPress function to get the URL. - If it’s an array (sometimes JetEngine stores an array with
'url'key), it uses the'url'value. - If it’s a string (raw URL), it escapes and uses it directly.
So the snippet safely covers all these cases.
If your image is not showing, verify:
- The repeater meta field data exists and has the expected structure.
- The image ID is valid and the image is accessible.
- You can add debugging code temporarily like:
error_log( print_r( $first_item['it_staff_image'], true ) );to your function and check your debug logs to see what is stored.
If you want me to help troubleshoot your specific meta structure, just share the output of:
var_dump( get_post_meta( get_the_ID(), 'it-staff-repeater', true ) );inside a template or via
error_log.
Feel free to ask if you want me to adjust the function for your exact meta structure!
- If the stored value in
- User
It looks like this code is working but it's getting only the image from the first repeater item and replicating it to every other items. Can you fix that?
- Assistant
Got it — you want each repeater item in your Listing Item (or Elementor set of sections) to have its own background image based on the respective
it_staff_imagein that repeater row, instead of the same first image repeated everywhere.
What’s happening?
The code I gave runs once in
wp_head, detects the first repeater item only on the current post, and outputs CSS with that single image for the entire.it_staff_sectionclass. Since all your sections have that same class, they all get the same background.
How to fix?
You’ll need a dynamic approach per item instance in your listing, not a global style.
Suggested Approach
-
Give each Elementor section a unique class or data attribute identifying its repeater item index or ID.
-
Output or assign the background image URL dynamically inside the listing, per item, so each item’s background is specific.
Option A. Using a dynamic CSS inline style per item (preferred and simplest)
If you are outputting those items using a Listing Item (JetEngine/Elementor), you should be able to dynamically assign a background inline style at the section level for each repeater item, via JetEngine macros or Elementor Dynamic Tags.
Step-by-step solution example:
1. Add a HTML or Section container in your Listing Item with this custom attribute:
For example, add this attribute to the wrapper/container of the repeater item in the Listing Item:
<div class="it_staff_section" data-item-index="%item_index%">(Replace
%item_index%with the JetEngine or listing item macro/tag that outputs the current repeater index.)
2. Use a dynamic shortcode or function to output the background style inline per item:
Create a shortcode that gets the background URL for the current repeater item.
Example shortcode to use inside the Listing Item:
Add this code in your
functions.php:// Shortcode handler function it_staff_bg_shortcode( $atts ) { global $post; // Attributes: index of the repeater item (default 0) $atts = shortcode_atts( array( 'index' => 0, ), $atts, 'it_staff_bg' ); $index = (int) $atts['index']; $repeater_data = get_post_meta( $post->ID, 'it-staff-repeater', true ); if ( empty( $repeater_data ) || ! is_array( $repeater_data ) ) { return ''; } if ( ! isset( $repeater_data[ $index ] ) ) { return ''; } $item = $repeater_data[ $index ]; if ( empty( $item['it_staff_image'] ) ) { return ''; } $image = $item['it_staff_image']; if ( is_numeric( $image ) ) { $image_url = wp_get_attachment_image_url( $image, 'full' ); } elseif ( is_array( $image ) && isset( $image['url'] ) ) { $image_url = $image['url']; } else { $image_url = esc_url( $image ); } if ( ! $image_url ) { return ''; } // Return the inline CSS style attribute return 'style="background-image: url(' . esc_url( $image_url ) . '); background-size: cover; background-position: center center; background-repeat: no-repeat;"'; } add_shortcode( 'it_staff_bg', 'it_staff_bg_shortcode' );
3. Use that shortcode in your Listing Item element's wrapper attributes to apply the inline background style.
- In Elementor Listing Item editor, select the Section or wrapper widget.
- Find where you can add dynamic attributes or HTML tag attributes (some “HTML attributes” or “Advanced” > “Attributes”).
- Insert this shortcode with the current item index passed to it.
For example, if JetEngine or your listing theme exposes the current repeater index as a macro
%#%or{{_index}}, use that with the shortcode:[it_staff_bg index="{{_index}}"]or if the macro is
%CURRENT_INDEX%:[it_staff_bg index="%CURRENT_INDEX%"]
If Elementor/JetEngine does not support shortcode parsing in attributes, you can:
- Use a small inline PHP widget/HTML widget inside the Listing Item that outputs a container DIV with that style.
- Or, alternatively, output the style directly inside the template if you have the option.
What this achieves:
- Each listing/repeater item gets a unique inline style background image, no conflicts.
- No global CSS overwrites images on all sections.
- You keep Elementor control and your design intact.
Alternative (if you want a pure JS solution):
You could output data attributes with the image URLs on each section, then use JavaScript to apply background images accordingly. But PHP inline styles are more reliable and performant here.
Summary:
- The reason your code sets the same background image to all
.it_staff_sectionis because that selector applies globally, and only the first repeater image URL is injected. - Instead, assign inline styles per item in your repeater listing via a shortcode or dynamic tag that references the current repeater index.
- This gives each item its own unique background.
If you want, I can help you build the exact Listing Item setup to make this work with your macros or how your repeater index is provided. Just share what macros/tags JetEngine provides in your Listing Items.
Let me know if you want me to provide that!
-
- User
How to achieve this: Output or assign the background image URL dynamically inside the listing, per item, so each item’s background is specific.
- Assistant
Certainly! To output or assign the background image URL dynamically inside a JetEngine Listing, per repeater item, you should leverage the dynamic data of each repeater item inside the Listing Item template itself — typically by:
- Accessing the current repeater item's fields dynamically
- Applying inline styles or CSS classes accordingly
Here’s a detailed, step-by-step guide on how to assign a unique background per item:
1) Confirm your Listing setup
You must be using JetEngine Listing Items to display each repeater row.
- Your
it-staff-repeaterfield contains subfields:it_staff_image(media field, returns attachment ID or URL)it_staff_title(text)
- The Listing Item will represent EACH repeater item.
2) In your Listing Item Template (in Elementor):
JetEngine exposes dynamic macros/tags that let you output meta fields per repeater item.
You want to output the background image dynamically per Listing Item.
3) Use Dynamic Field widget with
it_staff_image- Drag an Elementor Section or Inner Section or Div widget that wraps your content in the Listing Item.
- Go to the Advanced > CSS Classes and give it a class, e.g.
it_staff_section. - Now, you want to assign a dynamic background image style per item.
4) Assign inline background image dynamically in Elementor
Unfortunately, Elementor Section background settings do not natively support dynamic background images from repeater meta fields.
Workaround:
Option A — Use an HTML widget or Elementor inner container with inline styles
Add an HTML widget inside your Listing Item, add this code:
<div class="it_staff_section" style="background-image: url('{{it_staff_image:url}}'); background-size: cover; background-position: center center; background-repeat: no-repeat;"> <!-- Your other content here --> </div>Explanation:
- Replace
{{it_staff_image:url}}with JetEngine's dynamic field macro that outputs the URL of the media field - JetEngine with Elementor allows curly brace macros like
{{field_name:modifier}}to output meta values dynamically - The
:urlmodifier tells JetEngine to output the image URL if the field is a media field.
If you want to keep styling consistent, wrap your content inside the above div.
If your field name is exactly
it_staff_image, this should work directly.
Option B — Use JetEngine Dynamic CSS or Code Snippet
If you want to keep the section element and not add extra DIVs:
- In the Listing Item, give your Section a unique CSS class like
it_staff_section_{{_index}}(use JetEngine's%_index%macro or equivalent for numbering items) - Then in your theme or via Code Snippet, output CSS styles per item using inline styles with indexes:
Example:
add_action( 'wp_head', function(){ if ( ! is_singular() ) return; global $post; $repeater = get_post_meta( $post->ID, 'it-staff-repeater', true ); if ( empty( $repeater ) || ! is_array( $repeater ) ) return; echo '<style>'; foreach ( $repeater as $index => $item ) { if ( ! empty( $item['it_staff_image'] ) ) { $image = $item['it_staff_image']; if ( is_numeric( $image ) ) { $url = wp_get_attachment_image_url( $image, 'full' ); } elseif ( is_array( $image ) && isset( $image['url'] ) ) { $url = $image['url']; } else { $url = esc_url( $image ); } if ( $url ) { echo ".it_staff_section_{$index} { background-image: url('{$url}'); background-size: cover; background-position: center center; background-repeat: no-repeat; }"; } } } echo '</style>'; });Then, in your Listing Item, make sure your section or container has class:
it_staff_section_0 it_staff_section_1 it_staff_section_2 ...depending on the repeater index.
(The way you assign the index class depends on what macros JetEngine makes available)
5) Using JetEngine Macros for Index or ID
Inside JetEngine Listing Item, check what dynamic macros you can use:
%_index%(zero-based repeating index)%_id%(item ID)- Custom macros available in your setup.
Assign a CSS class with that macro, e.g.,
it_staff_section_%_index%
Summary: Easiest way (Option A)
<div class="it_staff_section" style="background-image: url('{{it_staff_image:url}}'); background-size: cover; background-position: center center; background-repeat: no-repeat;"> <!-- Your content here --> </div>- Replace
{{it_staff_image:url}}with JetEngine dynamic field shortcodes/macros. - This applies the background per repeater item inside the Listing Item.
Troubleshooting:
- If
{{it_staff_image:url}}does not work, try just{{it_staff_image}}: sometimes the value is URL by default. - Preview in Elementor or on front-end to confirm the background images differ per item.
- You can inspect the HTML to verify each item’s container has the correct background style.
If you want, I can help write a minimal full example template code tailored to your exact setup — just share which field macros JetEngine exposes in your Listing Item editor.
Let me know if you want me to clarify or generate that example!
- User
Is it possible to achieve this using the Attributes feature from Elementor?
- Assistant
Yes! You can definitely use Elementor's Attributes feature (available in Elementor Pro on the Advanced > Attributes tab) to set dynamic attributes including inline styles, and thereby dynamically add the background image per listing item.
Here’s how to achieve dynamic background images for repeater items using Elementor's Attributes:
Step-by-step: Setting dynamic background per item with Elementor Attributes
1. In your Listing Item template, select the Section (or container) that should have the background
2. Go to Advanced > Attributes
You’ll see a field where you can add HTML attributes in the format:
attribute_name="attribute_value"
3. Add a style attribute with a dynamic background-image URL using JetEngine macros/dynamic tags
For example, in the Attributes field, enter:
style="background-image: url({{it_staff_image:url}}); background-size: cover; background-position: center center; background-repeat: no-repeat;"Notes:
- Replace
{{it_staff_image:url}}with the JetEngine macro that outputs the current repeater item's image URL. JetEngine typically supports{{field_name}}and{{field_name:url}}for media fields. - Make sure you add the entire style attribute, including the quotes, exactly like this.
4. Save & preview
Each Listing Item’s section will now have a unique inline style:
<section style="background-image: url(http://example.com/wp-content/uploads/your-image.jpg); background-size: cover; background-position: center center; background-repeat: no-repeat;"> ... </section>and the image URL will be different for each repeater item.
Additional tips:
- If you want to add a CSS class for common styling, you can still add a class under Advanced > CSS Classes, e.g.,
it_staff_section. - Add fallback background colors or other CSS rules in your theme for cases when the image is missing.
- Make sure caching and minification plugins do not break dynamic attribute rendering.
- Test the output on the front end with browser dev tools to ensure the style attribute is correctly output per item.
Summary
Element Option to set Example value written inside Elementor Attributes field Attribute: stylebackground-image: url({{it_staff_image:url}}); background-size: cover; background-position: center center; background-repeat: no-repeat;
Why use Attributes?
- It's native to Elementor (no PHP/code needed)
- Fully dynamic per repeater item inside Listing Items
- Easy to manage and maintain
- Keeps your layout clean; no extra HTML wrappers needed
If your JetEngine media field outputs just the ID and the URL macro doesn't work, you can verify by testing what
{{it_staff_image}}outputs in a text widget and adjust accordingly.
If you want, I can help you with screenshots or detailed instructions based on your JetEngine / Elementor versions.
Just ask!
- Replace
- User
When inspecting the console I'm seeing:
background-image: url({{it_staff_image:url}});
Is there anything else needed in order to make it work?
- Assistant
Ah, seeing
background-image: url({{it_staff_image:url}});literally in the HTML/CSS means Elementor is not parsing/replacing the dynamic tag/macros inside the Attributes field — it outputs it as plain text.
Why does this happen?
Currently, Elementor does not parse JetEngine macros (like
{{it_staff_image:url}}) inside the Attributes field by default.The Attributes field accepts Elementor Dynamic Tags (introduced in Elementor Pro 3.11+), but it supports Elementor’s own dynamic tags, not JetEngine curly-brace macros (
{{}}) directly.
How to fix this and make the background image dynamic inside Attributes?
Option 1: Use Elementor’s native Dynamic Tags system (set via UI)
If you want to dynamically set a style attribute in the Attributes field, Elementor itself requires you to insert dynamic tags via the UI, like in Text fields, Background fields, or even in some Attributes if supported.
But as of now, Elementor does not provide a dynamic tag in the Attributes input field to inject dynamic CSS inline.
Option 2: Use Elementor’s Dynamic CSS feature (Pro 3.11+)
From Elementor 3.11+, you can add Dynamic CSS snippets to elements, including setting dynamic background images — and you can reference custom fields here.
How-to:
- Select your widget or Section
- Advanced > Custom CSS (Pro feature)
- Insert CSS like:
selector { background-image: url("{{CUSTOM_FIELD:it_staff_image}}"); background-size: cover; background-position: center center; background-repeat: no-repeat; }Here
{{CUSTOM_FIELD:it_staff_image}}is Elementor’s dynamic tag syntax to grab ACF/JetEngine or meta fields.Note: This requires Elementor Pro 3.11+ with the dynamic CSS feature.
Option 3: Use JetEngine Dynamic Field to output inline style attribute
JetEngine offers a Dynamic Field widget where you can output any meta value.
Workflow:
- Add a Dynamic Field widget or widget allowing output of HTML attributes inside your listing item
- In Field, select your media repeater field's subfield (
it_staff_image) - Output format: Image URL
- Use the "Before HTML" and "After HTML" options to format it as inline style, e.g.:
Before HTML:
style="background-image:url(After HTML:
); background-size: cover; background-position: center center; background-repeat: no-repeat;"Place this Dynamic Field widget inside your Section wrapper or as an attribute via JavaScript.
Option 4: Use JS to parse markup and replace dynamic tags after page load (less ideal)
You could add a small JS snippet that searches for sections with
{{it_staff_image:url}}in their style attribute and replaces it with the actual image URLs fetched via AJAX or localized data.
Summary / Recommended approach:
Method Ease Requires Elementor Pro Notes Elementor Attributes + JetEngine macros No (doesn’t parse macros) Pro (Attributes supported) Not processed, outputs raw macros literally Elementor Dynamic CSS (Pro 3.11+) Yes Yes Best way if you have Elementor Pro 3.11+ JetEngine Dynamic Field output Moderate No You can build inline style with dynamic field outputs PHP shortcode inline styles (custom code) Yes No Full control but requires PHP coding
If you want a quick fix right now without Elementor Pro 3.11+:
Use a shortcode or JetEngine Dynamic Field widget, outputting the inline style per item dynamically, for example:
- Insert an HTML widget inside your Listing Item, and inside it add:
<style> .it_staff_section_{{_index}} { background-image: url('{{it_staff_image:url}}'); background-size: cover; background-position: center center; background-repeat: no-repeat; } </style>- Give your container a class like
it_staff_section_{{_index}}.
But you must verify that Elementor parses
{{it_staff_image:url}}in the text widget.
Want me to help you write a minimal working example using a shortcode or Elementor Dynamic Field widget output?
Let me know!
- User
It's not working. Isn't there any other way to do it using PHP?
- Assistant
Yes, you can definitely do this with PHP to inject per-item inline styles or unique classes with background images so that each repeater item in your Listing gets its correct background image.
Here’s a practical PHP-based approach, assuming:
- You have a Listing Item template rendering each repeater item.
- You can add custom CSS classes or inline styles per item dynamically using PHP.
- You can add a small helper shortcode or function to output the style attribute per item.
Step 1 — Add a shortcode that outputs inline style with the background image for the current repeater item
Add this code to your theme’s
functions.phpor a site-specific plugin:/** * Shortcode [it_staff_bg style] outputs inline style for background-image from the current repeater item index. * Usage: [it_staff_bg index="0"] */ function it_staff_bg_shortcode( $atts ) { global $post; $atts = shortcode_atts( array( 'index' => 0, ), $atts, 'it_staff_bg' ); $index = (int) $atts['index']; // Get repeater data meta $repeater = get_post_meta( $post->ID, 'it-staff-repeater', true ); if ( empty( $repeater ) || ! is_array( $repeater ) ) { return ''; } if ( ! isset( $repeater[ $index ] ) || empty( $repeater[ $index ]['it_staff_image'] ) ) { return ''; } $image = $repeater[ $index ]['it_staff_image']; if ( is_numeric( $image ) ) { // Image is an attachment ID $image_url = wp_get_attachment_image_url( $image, 'full' ); } elseif ( is_array( $image ) && isset( $image['url'] ) ) { $image_url = $image['url']; } else { $image_url = esc_url( $image ); } if ( ! $image_url ) { return ''; } return 'style="background-image: url(' . esc_url( $image_url ) . '); background-size: cover; background-position: center center; background-repeat: no-repeat;"'; } add_shortcode( 'it_staff_bg', 'it_staff_bg_shortcode' );
Step 2 — Add the shortcode inside your Listing Item container in Elementor
You need to pass the current repeater index to the shortcode.
How to do this depends on what JetEngine/Elementor macros you have to get the current index. Common ones:
%_index%{{_index}}- or something else.
Try this in your Section’s Advanced > Attributes field:
[ it_staff_bg index="%_index%" ]or
[ it_staff_bg index="{{_index}}" ](Without spaces, e.g.
[it_staff_bg index="{{_index}}"])If the Attributes field doesn’t parse shortcodes, do the following:
Step 3 — Output a container div with the shortcode inside HTML widget
- Add an Elementor HTML widget inside your Listing Item at the very top (or wrapping your section content)
- Put something like:
<div class="it_staff_section" <?php echo do_shortcode('[it_staff_bg index="{{_index}}"]'); ?>> <!-- Your content goes here --> </div>Since you cannot execute PHP inside Elementor HTML widget, an alternative is:
- Create a small shortcode that outputs the whole block including container:
/** * Shortcode to output container with background style and content. * Usage: [it_staff_item index="0"]Your content here[/it_staff_item] */ function it_staff_item_shortcode( $atts, $content = null ) { global $post; $atts = shortcode_atts( array( 'index' => 0, ), $atts, 'it_staff_item' ); $index = (int) $atts['index']; $repeater = get_post_meta( $post->ID, 'it-staff-repeater', true ); if ( empty( $repeater ) || ! is_array( $repeater ) ) { return do_shortcode( $content ); } if ( ! isset( $repeater[ $index ] ) || empty( $repeater[ $index ]['it_staff_image'] ) ) { return do_shortcode( $content ); } $image = $repeater[ $index ]['it_staff_image']; if ( is_numeric( $image ) ) { $image_url = wp_get_attachment_image_url( $image, 'full' ); } elseif ( is_array( $image ) && isset( $image['url'] ) ) { $image_url = $image['url']; } else { $image_url = esc_url( $image ); } $style = ''; if ( $image_url ) { $style = sprintf( 'style="background-image: url(%s); background-size: cover; background-position: center center; background-repeat: no-repeat;"', esc_url( $image_url ) ); } return sprintf( '<div class="it_staff_section" %s>%s</div>', $style, do_shortcode( $content ) ); } add_shortcode( 'it_staff_item', 'it_staff_item_shortcode' );Now in your Listing Item HTML widget you write:
[it_staff_item index="{{_index}}"] <!-- Your repeater item content here --> [/it_staff_item]Replace
{{_index}}with your actual macro for item index.
Important:
- Check which macros JetEngine exposes for the current repeater item index (
_indexis typical). - You can test an Elementor widget with just
{{_index}}printed to verify.
Summary
- PHP shortcode reads the repeater meta data for the given index.
- Outputs container div with inline background image style.
- Wraps your content dynamically.
- You have per-item dynamic background images with no global CSS.
If you want, I can help you further with exact JetEngine macro names or provide a minimal step-by-step Elementor setup for this shortcode.
Let me know if you want me to prepare that!