I’m not sure I understand you’re problem. I was under the impression that acf_form works the same in ACF4 and 5. If your still having problems can you explain in more detail so that I can understand what they are?
The editor I use hightlights all my typos that cause errors in php. It’s so easy to make tiny mistakes, like the single quote before the function call 😛
ACF 5 uses the ‘acf’ in place of ‘fields’. you need to change each $_POST['fields'][...
to $_POST['acf'][...
The '.
on each line is backwards, dyslexic typing and should be '.
Sorry, I don’t debug code I type here as examples.
$images = array(
'Apple' => 'get_template_directory_uri().'/dist/images/media/image-01.png',
'Orange' => 'get_template_directory_uri().'/dist/images/media/image-02.png',
'Pear' => 'get_template_directory_uri().'/dist/images/media/image-02.png',
)
$values = get_field('association');
foreach ($values as $value) {
?><img src="<?php echo $images[$value] ?>" alt=""><?php
}
It depends on what you’re trying to get. ACF stores the attachment ID for an image field. If you need to switch from get_field() to get_post_meta() then you’ll need to do any formatting that ACF was doing on your own using functions like https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/
I don’t see any reason why the values should be listed on different lines, you don’t have any <div>'s
or <br />'s
in there that would cause this. Perhaps the formatting issue is CSS related? for example if the <a>'s
are set to display as blocks or there’s some other CSS formatting that’s causing it.
Ok, that makes a log more sense, for a multi select you’d need to loop through the values. I would suggest setting up the images in an array first just to make life easier and to remove the if/elseif logic
$images = array(
'Apple' => 'get_template_directory_uri()'./dist/images/media/image-01.png',
'Orange' => 'get_template_directory_uri()'./dist/images/media/image-02.png',
'Pear' => 'get_template_directory_uri()'./dist/images/media/image-02.png',
)
$values = get_field('association');
foreach ($values as $value) {
?><img src="<?php echo $images[$value] ?>" alt=""><?php
}
Did you mean to mark my question as the answer? Sorry, I was wondering about the field names because field names cannot start with ‘field_’ and if they did that could have been a cause of the issue.
For JSON values you could use either a text field or a textarea field with not formatting. Or you could use any type of field that will hold text data and when getting the value set the 3rd parameter in get_field($field, $post_id, $format_value)
to false to make sure the value is not formatted or altered in any way.
A post object field that allows multiple selections, or a relationship field, both work basically the same way, it’s really only the interface that’s different.
Basically this is what you’d need to do. Let’s just say that the old field was named “old_field” and the new one is named “new_field”. You create an acf/load_value filter for the new ACF field, information can be found here https://www.advancedcustomfields.com/resources/acfload_value/
add_filter('acf/load_value/name=new_field', 'convert_old_field_to_acf', 10, 3);
function convert_old_field_to_acf($value, $post_id, $field) {
if ($value !== NULL) {
// if the value is not set for ACF yet
// then the value will be NULL
// if the current value is not NULL
// then return because the field already
// had been set
return;
}
// get value from old field
$old_value = get_post_meta($post_id, 'old_field', false);
if (!empty($old_value)) {
$value = $old_value;
}
return $value;
}
Using a filter like this you can leave all the old values where they are, they will be returned for use is ACF function and eventually all of the data will be move over as posts are edited so you don’t need to go an convert everything ahead of time.
Short answer is that you can’t. The problem is that the old plugin you used stored multiple values in multiple rows and ACF stores these types of values in a serialized array in a single DB entry. You will need to convert the value.
My suggestion would be to create a new field name for ACF, but how you proceed from there depends on what type of field you set up in ACF. What is the field type of the field you’re trying to use?
because I can’t spell
add_filter('acf/load_field/name=author_promo', 'acf_label_htmlentities');
function acf_label_htmlentities($field) {
$field['label'] = htmlentities($field['label']);
return $field;
}
Fields with JSON in them should work fine.
Are those the actual field names (starting with field_
)? If they are how did you create them?
If you want to show html then you’ll need to put the HTML entities into the label. Either that or filter the label when loading the field. Something like this should work
add_filter('acf/load_field/name=your-field-name', 'acf_label_htmlentities');
function acf_label_htmlentities($field) {
$field['label'] = htmleneities($field['label']);
return $field;
}
I’ll need to remember that. I’ve built a few widgets, just not with ACF yet.
It’s an easy mistake to make because the hook for posts is manage_posts_custom_column
and the logical assumption is to replace posts with your custom post type rather than add the post type in the middle somewhere. But then again, not everything in WP is logical.
in your code $widget_id
has no value. I’m not sure where to get it from in this context but it should be an argument in either $args
or $instance
. I’ve done some looking, and even looking and it’s hard to tell I think it may be $instance->id
but I’m not sure
“medium” is a default image size added by WP, so that image size should exist unless you’ve somehow removed it. If you need to filter the image that is returned then see https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/
the hook for showing you custom column contnet should look like
manage_{CUSTOM-POST-TYPE}_posts_custom_column
so if the CPT slug is portfolio your hook should be
manage_portfolio_posts_custom_column
I don’t understand how you are outputting even a single selection has an image? Where is the image tag coming from?
I thought of that after my comment, but thought there might be some reason you needed something different.
I looked at the plugin and I don’t see anything with the quick look that sticks out as being a problem. You might want to report this as a problem for that plugin. This type of thing has come up in the past.
In the mean time, you can fix this by testing to see if the value returned by get_field('repeater_field_name')
is a number of an array. If it is a number you can loop through the rows of a repeater manually to get the array and then use that array to show the content instead of using the have_rows() repeater.
// how to loop a repeater field (options) to get values with get_option()
$repeater = 'repeater_name';
$subfields = array('field_name_1', 'fiel_name_2', 'etc...');
$count = intval(get_option('options_'.$repeater));
$value = array();
for ($i=0; $i<$count; $i++) {
$value[$i] = array();
foreach ($subfields as $field) {
$field_value = get_option('options_'.$repeater.'_'.$i.'_'.$field);
// you may need to do additional formatting on the value here
// since this gets value stored in the database
// and is not formatted by ACF
$value[$i][$field] = $field_value;
}
}
Let me know about the plugin. If not that one please let me know where I can find the one that you’re using.
Also, are the only pages on the site that this is happening related to this plugin and you don’t see this on any other page of the site?
Have you tried deactivating other plugins on the site to see if the problem goes away?
I’d say to try and deactivate this plugin, but I’m assuming that the page will not load without it, you could still try.
The problem is not in ACF, so we need to work out where the incompatibility is.
For some reason, on this page, get_field is returning the number of rows in the repeater instead of the nested array of values that it should be returning. Something in interfering with the get_field on this page.
Is this the plugin that you’re using? https://wordpress.org/plugins/dsidxpress/
k,
Where you have ‘TEXT’ put
echo 'GET FIELD = ',get_field('second_buttons','option'),'<br>GET OPTION =',get_option('options_second_buttons');
Does that output anything? What?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.