Getting the field data (text and images) for specific sections in cloned groups
Support › Meta Box AIO › Getting the field data (text and images) for specific sections in cloned groups
- This topic has 7 replies, 2 voices, and was last updated 7 months, 1 week ago by
Peter.
-
AuthorPosts
-
September 12, 2024 at 8:14 PM #46426
Fredrik
ParticipantI'm using Metabox with Bricks and I want a function to populate a page with the field from specific clones in a Metabox Custom Post.
It works with this code for text:
function get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index = 0) { $meta_value = get_post_meta($post_id, $field_group, true); if (!is_array($meta_value) || !isset($meta_value[$clone_index])) { return null; } $cloned_group = $meta_value[$clone_index]; return isset($cloned_group[$field_name]) ? $cloned_group[$field_name] : null; } // Register the function as a Bricks dynamic data source add_filter('bricks/dynamic_data/register_sources', function($sources) { $sources['metabox_cloned_field'] = [ 'label' => 'Metabox Cloned Field', 'callbackFunction' => function($post_id, $field_group, $field_name, $clone_index) { return get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index); }, ]; return $sources; }); // Make sure the function works with Bricks dynamic data syntax add_filter('bricks/dynamic_data/render_content', function($content) { return preg_replace_callback('/\{echo:get_metabox_cloned_field\((.*?)\)\}/', function($matches) { $args = explode(',', $matches[1]); $args = array_map('trim', $args); $post_id = get_the_ID(); // Always use the current post ID $field_group = trim($args[1], "'\""); $field_name = trim($args[2], "'\""); $clone_index = isset($args[3]) ? intval($args[3]) : 0; return get_metabox_cloned_field($post_id, $field_group, $field_name, $clone_index); }, $content); }, 10, 1);
I'm wondering if this will be sustainable when Metabox is updating or if there's a better approach?
And I'm wondering if you can guide me on how to get the equivalent of images?
Right now I got this code for images, but it doesn't work:
function get_metabox_cloned_image($post_id, $field_group, $clone_index = 0, $image_index = 0, $size = 'full', $debug = false) { $meta_value = get_post_meta($post_id, $field_group, true); if ($debug) { echo '<pre>'; print_r($meta_value); echo '</pre>'; return; } if (!is_array($meta_value) || !isset($meta_value[$clone_index]['images'])) { return null; } $images = $meta_value[$clone_index]['images']; if (!isset($images[$image_index])) { return null; } $image_id = $images[$image_index]; return wp_get_attachment_image_url($image_id, $size); } add_filter('bricks/dynamic_data/register_sources', function($sources) { $sources['metabox_cloned_image'] = [ 'label' => 'Metabox Cloned Image', 'callbackFunction' => function($post_id, $field_group, $clone_index, $image_index, $size = 'full', $debug = false) { return get_metabox_cloned_image($post_id, $field_group, $clone_index, $image_index, $size, $debug); }, ]; return $sources; }); add_filter('bricks/dynamic_data/render_content', function($content) { return preg_replace_callback('/\{echo:get_metabox_cloned_image\((.*?)\)\}/', function($matches) { $args = explode(',', $matches[1]); $args = array_map('trim', $args); $post_id = get_the_ID(); $field_group = trim($args[1], "'\""); $clone_index = isset($args[2]) ? intval($args[2]) : 0; $image_index = isset($args[3]) ? intval($args[3]) : 0; $size = isset($args[4]) ? trim($args[4], "'\"") : 'full'; $debug = isset($args[5]) ? (trim($args[5], "'\"") === 'true') : false; return get_metabox_cloned_image($post_id, $field_group, $clone_index, $image_index, $size, $debug); }, $content); }, 10, 1);
September 12, 2024 at 11:15 PM #46436Peter
ModeratorHello Fredrik,
It is sustainable to use the WordPress function
get_post_meta()
to get the field value from the tablewp_postmeta
when Meta Box is updated or even deactivated.Regarding the image, which is the field type that you are using?
September 12, 2024 at 11:18 PM #46437Fredrik
ParticipantGood!
It's Image advanced, where I need a function to grab either the 1st, 2nd, 3rd and so on of a specificed clone.
September 13, 2024 at 10:48 PM #46450Peter
ModeratorHello,
Can you please export the field group to a JSON file and share it here? I need to check the settings of group and image advanced fields to give a better suggestion to get the field value.
Following the documentation https://docs.metabox.io/extensions/meta-box-builder/#export--importSeptember 14, 2024 at 2:46 AM #46455Fredrik
ParticipantSure, this is what it looks like in the code view
<?php $groups = rwmb_meta( 'aspects' ); foreach ( $groups as $group ) { // Field heading: echo $group[ 'heading' ] ?? ''; // Field accent_heading: echo $group[ 'accent_heading' ] ?? ''; // Field text: <?php $values = $group[ 'text' ] ?? []; <?php foreach ( $values as $value ) : ?> <div class="my-content"><?= $value ?></div> <?php endforeach ?> // Field images: $image_ids = $group[ 'images' ] ?? []; ?> <h3>Uploaded images</h3> <ul> <?php foreach ( $image_ids as $image_id ) : ?> <?php $image = RWMB_Image_Field::file_info( $image_id, [ 'size' => 'thumbnail' ); ?> <li><img src="<?= $image['url']; ?>"></li> <?php endforeach ?> </ul> <?php } ?>
September 15, 2024 at 9:35 PM #46461Peter
ModeratorHello,
I see that the function
get_metabox_cloned_image()
to get the image advanced field value in a cloneable group is correct. But I'm not sure about the code in 2 filter hooksbricks/dynamic_data/register_sources
andbricks/dynamic_data/render_content
. Maybe you can ask the Bricks support to get further assistance.September 16, 2024 at 10:43 PM #46480Fredrik
ParticipantI forwarded this to Bricks Support and their response is that custom codiing is generally outside of their support scope, but that:
But, if you have an image, and you call custom function
{echo:get_my_image}
, and this function returns ID of the image (media file), it will show it.So
{echo:custom_image}
Together with this function
function custom_image() { return 162; }
Will render the image with an id of 162.
Does that help with figuring out the last bit for you?
September 18, 2024 at 10:15 PM #46494Peter
ModeratorHello,
As I mentioned above, I'm not sure how the filter of Bricks works on their side. If it is possible, you can try to use the theme code generated by Meta Box, use a WordPress theme (2021), add the code to the post template and see if the image advanced is outputted in the frontend.
If it works properly, then it isn't an issue with Meta Box itself. -
AuthorPosts
- You must be logged in to reply to this topic.