@mike3deep No problem, glad it worked! But will you please mark my answer above as the post that solved this question so others can find it easier.
This guide will show you how to dynamically populate a select fields options. You can use whatever WordPress queries you’d like to get the list of users that you’re after, and then use this guide to implement it into the select field.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Your Return Value must be set to Both (Array). That way your field will be an array that contains both the value and the name for each choice, which is what you’re looking for.
Once you have that, try this code out as an example:
<?php
$colors = get_field('features');
if( $colors ): ?>
<ul>
<?php foreach( $colors as $color ): ?>
<li class="<?php echo $color['value']; ?>"><?php echo $color['label']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
It looks like you may have found an issue that needs to be updated in ACF in regards to the avatar image. I tried this code as well and got the same results that you did.
Here’s why it wasn’t working:
– The code is using get_avatar
which returns the avatar, but in an <img>
element. So using that within the src
attribute of another <img>
is the issue here.
– Simply change ‘get_avatar’ to get_avatar_url
which will only return the image’s source. See my example below.
– The user’s link isn’t working because that needs to be manually entered on the User’s profile. Go to a User’s profile in the admin and you will see a field named “Website”, this is what will be returned when using $user->user_url
from the example code.
– I’ve added a conditional to my code example below, that will only wrap the User’s name in an <a>
tag, if they have a $user->user_url
stored on their profile. Otherwise it will just print out their name as text.
Here’s my example of the code, modified to fix the issue:
<?php
$users = get_field("volunteers");
if( $users ): ?>
<ul class="volunteers-list">
<?php foreach( $users as $user ): ?>
<li>
<img src="<?php echo esc_attr(get_avatar_url($user->ID)); ?>" alt="author-avatar"/>
<?php if($user->user_url) : ?>
<a href="<?php echo esc_attr($user->user_url); ?>"><?php echo $user->display_name; ?></a>
<?php
else :
echo $user->display_name;
endif;
?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Post the code you’re using. You mentioned that you followed the steps in the documentation, but perhaps you have something wrong the we can identify.
This is exactly what the Image Array was designed for. You’re essentially doing it all manually when you don’t need to.
Look at the docs: https://www.advancedcustomfields.com/resources/image
Make sure that your Image field has the Return Format set to Image Array, then use the following code as an example:
<?php
$image = get_field('image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo
esc_attr($image['alt']); ?>" />
<?php endif; ?>
Please show your code outputting these fields in your PHP.
Well that’s good. But since the new code you posted does work, I’d start checking things like viewing your browsers source code for the page and verify that your custom script is indeed loading after ACF’s acf-input.js
. Perhaps there is a problem there, even though your wp_register_script
is setup properly.
Also a theme can cause odd behaviors on plugins depending on what it’s trying to do. I’m just in the middle of developing an add-on plugin to ACF and when I popped that code in, it worked great.
I’m sorry I can’t be of more help at this point, but it seems like you DO have the file enqueued properly, just a matter of why the acf
object isn’t able to be accessed. Which is odd, because ACF attaches that object onto window
, so it should be available anywhere.
I just tested out that exact code and it worked just fine. You don’t need PHP to interact with ACF’s javascript API. So accessing it with the global acf
object like you are, should work fine.
The issue must be elsewhere, perhaps in your path to the script?
Can you log out other things successfully to verify that your script is enqueued properly?
Try logging out a string like: console.log('testing')
does that work alright so we know the enqueue is correct?
The action you’re using, 'acf/input/admin_footer'
only fires on the admin page where you edit the field groups. (Perhaps this is not what you want)
To enqueue a script that fires on every admin page and loads after ACF, use this:
wp_register_script('custom-acf-js', get_template_directory_uri() . '/library/scripts/script-js.js', ['acf-input'], '1.0.0', true);
wp_enqueue_script('custom-acf-js');
The five parameters in the 'wp_register_script'
are:
wp_register_script(UNIQUE_NAME, FULL_URL_OR_PATH, DEPENDENCIES_ARRAY, VERSION, LOAD_IN_FOOTER_BOOLEAN);
wp_enqueue_script('custom-acf-js')
as seen here: https://developer.wordpress.org/reference/functions/wp_register_script
Putting ['acf-input']
in the dependencies array parameter tells the script it needs to wait until ‘acf-input’ has loaded first, which is ACF’s js script.
If you want the script to load in other ways, on other pages, ACF also has other actions you can hook into, take a look here for those: https://www.advancedcustomfields.com/resources/#actions
If Field 3 is also a radio button field, how can it hold both values of Field 1 & 2? Are field 1 & 2’s values numerical and Field 3 is always supposed to be the sum of those two other fields?
There’s really no simple way of doing this. In the ACF core javascript, the new element is appended to the list, and I don’t really see a way of hooking into that easily. If this is a client’s request, the short answer is no. The long answer is yes, but it’ll cost them some $$$.
Basically you’d need to hijack what is natively done and things can get sticky from there. Or…. you can jump into the ACF core javascript files and change this: this.$list('values').append( html )
to this.$list('values').prepend( html )
. But of course, that’ll only last until they update ACF, which is horrible idea.
No problem. Glad you got it worked out.
I see the problem now!! I missed it on your previous post.
Ok, in your “Test” code above, you need to change:
if( get_sub_field($product_type_value) == 'fireplace_wood' )
to just:
if( $product_type_value == 'fireplace_wood' )
Because your $product_type_value
is already storing the value from the sub_field. So you don’t pass that back into your sub_field.
You should be good to go now!
Below $product_type_array = get_sub_field( 'product_type' );
put var_dump(get_sub_field( 'product_type' );
and let me know what it says.
If it’s empty also try var_dump(get_field( 'product_type' ));
and let me know.
You’ll definitely need those conditionals in your PHP code.
From what I can tell, it looks like you need change:
get_field('product_type')
to get_sub_field('product_type')
Reason being, is it looks like from your structure, that “product_type” is a sub_field of the Specifications field (which is a “Group” field type), right? If so, that should do it.
I recommend the WP All Import plugin along with its ACF add-on and WooCommerce add-on. I use those plugins all the time to import data and it’s always worked out great!
In your textarea field settings there is a setting labeled New Lines, set it to No Formatting and it will not wrap anything in <p>
tags.
Isn’t each line in the WYSIWYG already wrapped in a <p>
tag? Could you accomplish what you’re trying to do by targeting/styling the <p>
tags instead of wrapping them in a <span>
? What is your reason for wrapping in a <span>
?
I’m sorry if this isn’t answering your question directly, but for things like this, wouldn’t it be simpler to just assign a class to the layout and then style that class appropriately?
For instance:
<?php if( have_rows('pagebuilder') ): ?>
<?php while( have_rows('pagebuilder') ): the_row(); ?>
<?php if( get_row_layout() == 'page_mainteaser' ): ?>
/* Site-Mainteaser */
// I made these attributes multiline to read easier on these forums
<div id="mainteaser"
style="background-image: url('<?php the_sub_field('background'); ?>')"
class="headline-<?php the_sub_field('color_headline'); ?>
text-<?php the_sub_field('color_text'); ?>"
>
<!-- Layout content here... -->
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Then in your stylesheet add corresponding styles like:
.headline-red h1 { color: red }
.headline-blue h1 { color: blue }
.headline-green h1 { color: green }
.text-black p { color: black}
.text-white p { color: white }
This doesn’t seem to be an issue with ACF, rather you should contact the Enhanced Fields developer and see if you can get some insight there.
I have a few ideas on why, but to be sure, can you please post your code that is displaying both the working and non-working fields please.
The =
in if ( $page_id = $employee_subject_description_subject->ID )
should be changed to ===
to compare that the two values are equal. Your single =
is actually assigning the value of $employee_subject_description_subject->ID
to the $page_id
variable.
Whoops! Sorry, I mistook each $director
in $team['Directors']
to be an array when each is actually an object. So our solutions are actually the same, except yours is using the proper syntax for a property on an object $director->name
, where I was using the syntax for a property on an array $director['name']
. I hope my mistake didn’t cost ya too much time, sorry about that.
I hope that clarifies though why one works now and the other threw an error. Also, your code looks just great, but you don’t need the $commaArray = $director->name;
line.
I’m glad you got it worked out!
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.