• Resolved swarajaccustra

    (@swarajaccustra)


    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?

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    Thread Starter swarajaccustra

    (@swarajaccustra)

    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.

    [my_field_key]=> Array
    (
    [0] => a:0:{}
    )

    Plugin Author Navneil Naicker

    (@navzme)

    @swarajaccustra – I would like to replicate this issue. Could you please share the code you are currently working on?

    Thread Starter swarajaccustra

    (@swarajaccustra)

    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.

    Plugin Author Navneil Naicker

    (@navzme)

    @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.

    1. Go to the WordPress plugin folder and find the folder named “acf-galerie-4”
    2. 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.
    3. Find this code
    	function format_value( $value, $post_id, $field ) {
    if ( empty($value) ) die();

    $attachment_ids = array_map( 'intval', $value );

    return $this->get_attachments( $attachment_ids );
    }

    4. Replace it with this code

    	function format_value( $value, $post_id, $field ) {
    $attachment_ids = array_map( 'intval', $value );

    if( empty( $attachment_ids ) ){
    return array();
    }

    return $this->get_attachments( $attachment_ids );
    }

    Do let me know if that’s what you wanted.

    Thread Starter swarajaccustra

    (@swarajaccustra)

    Thanks, worked. looking for the next stable update. Great plugin btw.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.