Hi Thomas,
It is working but when i am make more columns with the builder (like a new row with a 1 column layout) then i get again a same value on that box.
How can i make it so every box in every get_row_layout on the page get a unique number?
It seems that you loop the rows two times. If so save the last row_index in a variable and add this var to the row_index in the second loop.
It should look like this:
<?php
if( have_rows('boxes') ):
while ( have_rows('boxes') ) : the_row();
if( get_row_layout() == 'text' ): ?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'image' ): ?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'photo_slider' ): ?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php // on the last get_row_layout()
$r = get_row_index(); ?>
<?php endif; endwhile; else : endif; ?>
<?php // start the second loop
if( have_rows('boxes') ):
while ( have_rows('boxes') ) : the_row();
if( get_row_layout() == 'text' ):
$r = $r + get_row_index(); ?>
<div class="box field-class-<?php echo $r; ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'image' ):
$r = $r + get_row_index(); ?>
<div class="box field-class-<?php echo $r; ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'photo_slider' ):
$r = $r + get_row_index(); ?>
<div class="box field-class-<?php echo $r; ?>">
<?php the_sub_field('content'); ?>
</div>
<?php endif; endwhile; else : endif; ?>
good to know if the standard WP registration form could be made to follow the same required fields format/js as ACF, as the WordPress default does it server side and doesn’t put a * next to the fields like ACF.
We also prefer this. Currently, if you have an ACF required field, ACF has good validation. But if you forget the mandatory E-Mail field of WordPress then it submits and returns your form back Empty…. this could really be a good addtion to ACF to hook into this on the Front end / user registation or edit Profile pages.
I figured out what’s going wrong:
In the ACF RGBA Color Picker the apply_filters( "acf/acfrb_color_picker/palette", true )
in set in the __construct()
of the field class in the $this->settings = array()
. This setting is later used in a wp_localize_script()
function in the function input_admin_enqueue_scripts()
That seems to be to early. I moved the apply_filters...
directly in the localized script part and now it works.
Maybe you get an idea why this is working if setting the fields with ACF field groups page and not working if fields created via PHP.
Thanks again for your help!
John, thanks for your testing.
I have checked my database and also checked if I have double field names (because I created the fields via PHP). No errors, no doubles.
To be sure, I deleted all entries from the database belonging to my options page (no worries, I’m in a testing environment).
After that, I added two new colors to my repeater and checked the database again. Everything is fine.
But I was surprised when I tried to read and print the repeater field this way
<?php
$test = get_field('MY_REPEATER', 'MY_OPTIONS_PAGE');
echo "<pre>";
print_r($test);
echo "</pre>";
?>
I only get the number 2
, the count of repeater rows.
Then I create a repeater with fields in the backend (not via PHP) and tested this. And yes, it works!!!
I exported this field groups to get the code and copy this into my functions.php to create the field groups via PHP to test if I make any mistakes on my first try.
And that’s the next surprise: Now it does not work!!!
With the above test I only get the number of the repeater rows and no array.
So it must have to do anything with registering the field groups via PHP.
I was curious, so I created an options page using the following
$args = array(
'title' => 'MY_OPTIONS_PAGE',
'menu_title' => 'MY_OPTIONS_PAGE',
'menu_title' => 'MY_OPTIONS_PAGE',
'post_id' => 'MY_OPTIONS_PAGE'
);
Then I create a test field group with just a text field named “where_am_i”. I saved a value to it and then search the db for the value I save. It turned up in the options table with an option name of MY_OPTIONS_PAGE_where_am_i
. I also tried some other field examples including a repeater field and they all end up in the options table with the prefix of MY_OPTIONS_PAGE_
and everything worked as expected, although I’m not exactly sure how because for me this is unexpected behavior (but intriguing since this opens up things I was not aware of)
Anyway, this leads to the question of, if it does work, why isn’t it working for you.
The only answer that I can come up with here is that you have another field that also evaluates to the option name of MY_OPTIONS_PAGE_MY_REPEATER...
or that somehow your filter is interfering with the working of your have_rows() loop, though I can’t say why. When I run the code for the options in my page template on the site everything is working.
For your last example try something like
<?php
function set_acf_rgba_color_picker_palette() {
// optional - add colors which are not set in the options page
$palette = array(
'#FFF',
'#000'
);
// ====================================================================
// Try to the repeater as an array
// ====================================================================
$test = get_field('MY_REPEATER', 'MY_OPTIONS_PAGE');
echo "<pre>";
print_r($test);
echo "</pre>";
if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
while( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) { the_row();
$palette[] = get_sub_field('MY_COLOR_FIELD');
}
}
return $palette;
}
add_filter('acf/rgba_color_picker/palette', 'set_acf_rgba_color_picker_palette');
?>
This code is from the Repeater Field documentation and should do what you want:
<?php
$rows = get_field('slideshow'); // get all the rows
$rand_row = $rows[ array_rand( $rows ) ]; // get a random row
$rand_row_image = $rand_row['slideshow-image']; // get the sub field value
$image = wp_get_attachment_image_src( $rand_row_image, 'large' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
What did you get if you
<?php
echo "<pre>";
print_r($hero);
echo "</pre>";
?>
BTW: There’s one index to much echoing the $hero parameters:
<?php
// vars
$hero = get_field('hero');
if( $hero ): ?>
<div id="hero">
<img src="<?php echo $hero['url']; ?>" alt="<?php echo $hero['alt']; ?>" />
</div>
<?php endif; ?>
Have anyone deleted the post_meta files from my custom fields that are without post_id?
Is dangerous?
SELECT * FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL
is there any query to make that?
I think the get_row_index()
function will do the work
<?php
if( have_rows('boxes') ):
while ( have_rows('boxes') ) : the_row();
if( get_row_layout() == 'text' ):
?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'image' ): ?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php elseif( get_row_layout() == 'photo_slider' ): ?>
<div class="box field-class-<?php echo get_row_index(); ?>">
<?php the_sub_field('content'); ?>
</div>
<?php endif; endwhile; else : endif; ?>
the only requirement for the plugin is that the field names are the same
Hi @avegas
Please change your code to:
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
$title = get_field("h1_title", "product_cat_" . $product_cat_object->term_id);
return $title;
}
Hi @semlohj
Please open a new support ticket via https://support.advancedcustomfields.com/new-ticket/ so that this can get to Elliot quicker.
Hi @amasen
Since ACF version 5.6 ACF now makes use of select2 version 4 by default, perhaps this could be the cause.
Please enqueue a different version of this library by making use of the acf/settings filter and check if the issue is still replicated: https://www.advancedcustomfields.com/resources/acf-settings/
I misread your OP, it sounded to me like it was working the way it’s supposed to work.
I just did a test and can confirm that’s it is working correctly on my test site.
The validation is done using JavaScript and AJAX. Any error in JS or during the AJAX request can cause the request to fail and this might cause what you’re seeing.
The first step is to deactivate other plugins and possibly switch themes to narrow down what might be causing the conflict.
Hi John, thanks for taking the time to reply. It doesn’t answer my question however, maybe i didn’t state the situation correctly.
I’ve added ACF fields to user profiles, and deliberately marked them as required, because I want admins of the website to fill these out.
However, the required-setting doesn’t seem to do anything. Admins can leave these fields empty while updating user profiles, and no alert is given.
This shouldn’t be the case, right?
When you mark a field as required that is exactly what should happen, the field is required to have content or it will cause an error. If you want to be able to have fields with no value entered then they should not be marked as required.
There is a 3rd party plugin that does this https://wordpress.org/plugins/quick-and-easy-post-creation-for-acf-relationship-fields/. Other than that I probably wouldn’t look for this to be incorporated into ACF any time soon. You could try submitting a support ticket here https://support.advancedcustomfields.com/new-ticket/.
Hey,
is there any new infos about this feature request? 🙂
Greets
I’ve used a version of this code for a site I’m developing at the moment. It’s working exactly how I’d like appart from that I’d like to change the size of the avatar image from the standard 96px to 170px. Could someone advise how to edit the code below to achieve this – Thanks!
<?php $users = get_field('practitioners');
if ($users): ?>
<?php foreach ($users as $user): ?>
<?php setup_postdata($user);
$userid = $user['ID'];
$userfirst = $user['user_firstname'];
$userlast = $user['user_lastname'];
$useravatar = $user['user_avatar'];
$userdescript = $user['user_description'];
?>
<div class="authorWrap">
<?php echo $useravatar; ?>
<?php echo $userfirst; ?> <?php echo $userlast; ?> -
<?php echo $userdescript; ?>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Thanks!
Came from Google. Hello.
So does this mean that the field name was used for the other plugin? ACF just uses the the name as a key for queries related to the post (get_field)?
I guess it would depend on what’s in your repeater. If there is a lot of rows and a lot of data it could take a while. The more data you have the longer subsequent calls will take. Also, the amount of HTML that your generating could also effect the load speed. My example is only a very basic one that takes the html returned and stuffs it into another element and html is not a compact, quick loading format. For something with a lot of data and a lot of html generated it would probably be better to return the data as a JSON object and then build the HTML on the client side using JavaScript instead of generating it with PHP.
+1 for this functionality in the core of ACF.
I’m also currently using a workaround (jQuery) to apply the classes to the TinyMCE body based on the class that’s applied to the parent ACF field.
I’d like to give it a try with the plugin (post-2-post), just unsure what the setup would be and how I would pull the values
Here is my current setup:
The two post types are Product (‘product’) and Portfolio (‘featured_item’)
What would I need to change in my original code to pull the related post IDs using the plugin?
@appnet Use an acf/validate_value filter similar to what I suggested here https://support.advancedcustomfields.com/forums/topic/check-number-when-saving/
There may be a way to do this by altering the select2 arguments, see the select2_init
section. Unfortunately, I don’t know if this is possible or how to do so if it is.
Beyond this it would take the developer adding this feature. You can request this by submitting a new ticket here https://support.advancedcustomfields.com/new-ticket/
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.