- This topic has 15 replies, 3 voices, and was last updated 3 years, 8 months ago by Tom.
-
AuthorPosts
-
August 10, 2020 at 3:34 am #1395053Toby
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!
August 10, 2020 at 4:28 am #1395138DavidStaffCustomer SupportHi 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.
August 12, 2020 at 2:19 am #1398380TobyThanks 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?
August 12, 2020 at 2:20 am #1398381Toby(hopefully it goes without saying that I am using the actual name of the custom field in place of
fieldname
!)August 12, 2020 at 6:30 am #1398756DavidStaffCustomer SupportTry changing:
$fieldname_url = $fieldname['url'];
to
$fieldname_url = $embed_file['url'];
August 13, 2020 at 1:02 pm #1401553TobyNope, it is still returning ‘h’…
August 13, 2020 at 4:57 pm #1401797DavidStaffCustomer SupportWhats the ACF field name, Is it really
fieldname
?$embed_file = get_field( 'fieldname', $current_post );
August 14, 2020 at 4:46 am #1402403TobyNope, 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?
August 14, 2020 at 5:11 am #1402435DavidStaffCustomer SupportTotally – does it work ?
August 14, 2020 at 5:12 am #1402437TobyNo, that’s the reason I’m here 🙂
August 14, 2020 at 9:30 am #1402945TomLead DeveloperLead DeveloperThere’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);
August 15, 2020 at 10:58 am #1404284TobyNothing 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>
— that’s right, isn’t it? Referencing the shortcode like that?
August 15, 2020 at 10:59 am #1404285TobyI’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.
August 15, 2020 at 1:10 pm #1404381TomLead DeveloperLead DeveloperThe
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?August 15, 2020 at 1:19 pm #1404386TobyCorrect — 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.
-
AuthorPosts
- You must be logged in to reply to this topic.