[Support request] Elements header breaks ACF return values

Home Forums Support [Support request] Elements header breaks ACF return values

Home Forums Support Elements header breaks ACF return values

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #1395053
    Toby

    I have created a header element for a type of page and I want to return the value of a custom field (created using Advanced Custom Fields plugin) in that header.

    I am using the template tag {{custom_field.filename}} in the header element to call the field.

    The field in question, filename, should return the URL of that file. It works correctly when I include it in the body of a post. But when I use it in the header element, it incorrectly returns the ID of the file instead. In other words, it ignores ACF’s ‘Return Value’ setting.

    I’ve tried various fixes in case it’s a temporary issue: deleting and re-adding the field, calling it using a shortcode instead of the tag, and so on. Nothing works. ‘Return Value’ gets ignored.

    I found this discussion on the ACF support forum (https://support.advancedcustomfields.com/forums/topic/image-returns-id-no-matter-what-return-value-is-set/) which suggests that it is an issue with the theme. This seems logical, since it works fine on the body of the page, just not in the header element.

    Can anyone help? Thanks in advance!

    #1395138
    David
    Staff
    Customer Support

    Hi there,

    try creating a shortcode like this:

    
    add_shortcode( 'acf_element_image', function() {
    	
        $current_post = get_queried_object();
        
        $acf_element_image = get_field( 'acf_element_image_fieldname', $current_post );
        $acf_element_image_html = '<img src="' . $acf_element_image['url'] . '">';
    
        return $acf_element_image_html;
    	
    } );

    Should work as the missing factor is Elements are their own post type, so the get_queried_object should return the current single pots fields.

    #1398380
    Toby

    Thanks for your helpful reply!

    I added the following code based on the example you gave:

    <?php
    
    add_shortcode( 'embed_file', function() {
    	
        $current_post = get_queried_object();
        
        $embed_file = get_field( 'fieldname', $current_post );
        $fieldname_url = $fieldname['url'];
    
        return $fieldnameurl;
    	
    } );
    ?>

    This makes a bit of progress, but what gets returned seems purely the letter ‘h’, not the correct URL. I have experimented with different fields but always the letter ‘h’ is returned and nothing else.

    I have no idea why! Or where the ‘h’ is coming from. Perhaps it is just returning the first character of the URL (“https…”)?

    I have to admit that I think I am out of my depth here… I suspect the problem is probably with my amateurish adaptation of your code rather than with the theme. But nonetheless, can you help?

    #1398381
    Toby

    (hopefully it goes without saying that I am using the actual name of the custom field in place of fieldname!)

    #1398756
    David
    Staff
    Customer Support

    Try changing:

    $fieldname_url = $fieldname['url'];

    to

    $fieldname_url = $embed_file['url'];

    #1401553
    Toby

    Nope, it is still returning ‘h’…

    #1401797
    David
    Staff
    Customer Support

    Whats the ACF field name, Is it really fieldname ?

    $embed_file = get_field( 'fieldname', $current_post );

    #1402403
    Toby

    Nope, sorry, I was just trying to simplify. The actual field is called report_file (and I will have another one with a different name) and so my literal code is:

    <?php
    
    add_shortcode( 'embed_file', function() {
    	
        $current_post = get_queried_object();
        
        $embed_file = get_field( 'report_file', $current_post );
        $report_file_url = $embed_file['url'];
    
        return $report_file_url;
    	
    } );
    ?>

    Do you think the field name will make a difference?

    #1402435
    David
    Staff
    Customer Support

    Totally – does it work ?

    #1402437
    Toby

    No, that’s the reason I’m here 🙂

    #1402945
    Tom
    Lead Developer
    Lead Developer

    There’s likely something wrong with the way the field is set up.

    What does it output if you do this?:

    $report_file_url = $embed_file['url'];
    var_dump($embed_file);
    #1404284
    Toby

    Nothing additional.

    The output html is:

    <a class="button" href="">Evidence review report</a>

    and this doesn’t change whether I include the var_dump($embed_file); line or not.

    For reference, the code I’ve used in the element is:

    <a class="button" href="[embed_file]">Evidence review report</a> &nbsp;

    — that’s right, isn’t it? Referencing the shortcode like that?

    #1404285
    Toby

    I’m becoming increasingly confident that the cause of the problem is something stupid I’ve done because of my half-understanding of how the code works.

    #1404381
    Tom
    Lead Developer
    Lead Developer

    The var_dump() line should give us some debugging information – it should tell us what the $embed_file variable contains so we can use it properly.

    Does adding var_dump() not output anything on the page at all? No difference between having it and not having it?

    #1404386
    Toby

    Correct — no output at all, no difference between having it and not having it.

    I would agree with your guess that the field is not set up correctly… except that it does work correctly if I set it up (in the CPT UI plugin) to return the ID of the file rather than its URL. When I do that (and use the {{custom_field.report_file}} token instead of the custom shortcode), it correctly returns the file ID.

    So I figure there must either be something wrong with the shortcode code, or a bug in the CPT UI plugin that means it doesn’t correctly return the URL even when i choose that option.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.