Hello! I’m currently working with a custom template. I’ve set up a custom image gallery field using the plugin, but I’m encountering an issue. When I try to access the fields on the front end using get_fields(), the page breaks if there are no images attached to the gallery. There are times when I don’t want to attach any images. How can I resolve this error?
Error Message? If you try to read the field value when its empty its possible to get a error. You could check it and if the value is null or not set you skip the gallery building.
The problem is that there’s no error message, which makes it difficult to troubleshoot. If there are no images in the gallery, the entire get_fields() function stops working. I can’t simply skip the gallery field because get_fields() doesn’t return anything in that case. Ideally, it should at least return null so that I can detect the missing gallery field and handle it.
How did I check this? By using WordPress’s default meta fields function, which returned the expected result.
I’ve created a custom page template (page-custom.php)
<?php /* Template Name: Custom Page Template */
get_header();
$id = get_the_ID();
//print_r($id);
$fields = get_fields($id);
print_r($fields);
get_footer(); ?>
In ACF I’ve two custom field groups.
In “Custom Fields” Group, I’ve a text field called “Location”. In “General” Group, I’ve a galerie 4 field called “Photo Gallery”.
In Location, My input text is “India”. When I add images to Gallery field. I get both Location and Photo Gallery fields using get_fields() function. If there’s no image, I don’t get anything. Atleast, I should get the location field.
This reply was modified 1 year, 1 month ago by swarajaccustra.
@swarajaccustra – I have managed to replicate the issue. I will push the change in the next plugin update which will be late this month because I will have to do entire codebase testing before pushing to everyone.
If you want the solution now then you do this change in the acf-galerie-4 plugin file.
Go to the WordPress plugin folder and find the folder named “acf-galerie-4”
Open the folder “acf-galerie-4” and inside the folder you’ll see “class-acfg4-register-field-type.php” file. Open this file in a text/code editor.
Find this code
function format_value( $value, $post_id, $field ) { if ( empty($value) ) die();