Add a wrapper id
for me its sc_city
then add this jquery
$($("#sc_city option")[0]).text('Select city');
I think you should use ksort – it sorts the array by key.
Also you can’t save it to the $field[‘choices’], because this function does it by pointers. Just do it like this:
function sort_checkbox( $field ) {
ksort($field["choices"]);
return $field;
}
add_filter('acf/load_field/key=field_5d4164d40898f', 'sort_checkbox');
Or, I guess, a clue to how I could target these things in jQuery ‘change’ events
Its this bit in the blog sidebar that seems to causing the issue.
while ($latestposts->have_posts()) : $latestposts->the_post(); ?>
<?php the_post_thumbnail( 'small-rectangle', ''); ?>
<h4><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h4>
<p><?php the_excerpt(); ?></p>
<hr>
<?php endwhile; ?>
If I remove this bit it works. Not sure how to edit this bit though (its a site I inherited, so I didn’t code this bit…
Hi I will try to explain my proposal
http://joxi.ru/1A5YVjkUw3vaWA
I have a problem when a content manager is lazy person and don’t want to write 2-3 paragraphs for short description of post (I made it by ACF field). I want change this problem like a developer. I’m solving this problem, but in a bad way and you could help fix it =)
Can you please change the field ‘Character Limit’ and make <input type=”range”> instead of <input type=”number”>. If you make a change, it will help users to set not only the upper limit of the available characters, but also the lower limit. This value, in my understanding should have two values, – min value and max value. By default let it be 0 value in both meanings.
– min value number can’t be more than max value number. (but if max value===0 it’s OK)
– if field will be Required, person can’t save file if numbers of symbols will be less than the specified value
I hope you will consider my suggestion and be able to implement it in the next version of the plugin. Thanks
You do not return
a value from function called in an AJAX request.
You echo
the value in the format that your success function is expecting.
https://codex.wordpress.org/AJAX_in_Plugins
Sorry misread your question, you want it showing in backend not just front end, ignore my answer!
if ($(".slide-carousel").length) {
var slidesliders = document.querySelectorAll('.splide.slide-carousel');
for (var i = 0; i < slidesliders.length; i++) {
var slideslider = new Splide(slidesliders[i], {
type: 'loop',
perPage: 3,
updateOnMove: true,
gap: '4rem',
padding:'4rem',
autoHeight: true,
focus: 'center',
autoplay: false,
rewind: true,
arrows: true,
});
slideslider.mount()
}
}
Do I need both ACF and ACF PRO? No, ACF PRO is an independent plugin and does not require the free version to be installed. Once ACF PRO is active, you can deactivate the free version and any ACF premium add-ons.
I have solved several issues using the following:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
1 – ACF Field Setup
I have setup fields as FILED TYPE > LAYOUT > GROUP
https://www.advancedcustomfields.com/resources/group/
This allows us to target the fields with a given ACF field group using the field group name.
if( have_rows('group_name')){
while(have_rows('group_name')){
the_row();
if( $subfields = get_row()) {
I am outputting the custom post type, ACF data, to a table using a foreach loop.
The code is then displayed on our post using a [shortcode]
<?php
class CUSTOM_SHORTCODES {
public static function register_shortcodes() {
add_shortcode( 'custom_specs', array( __CLASS__, 'boat_specs' ) );
}
public static function custom_specs($atts) {
$out = '';
$out .= '<div>';
$out .= '<table>';
$out .= '<tbody>';
$subfields = get_row();
$field = get_sub_field_object( $key );
if( have_rows('specifications')){
while(have_rows('specifications')){
the_row();
if( $subfields = get_row()) {
foreach ($subfields as $key => $value) {
if ( !empty($value) ) {
if(is_array($value)) {
$value = @implode(', ', $value);
}
$field = get_sub_field_object($key);
$out .= '<tr class="tb-row"><td class="tb-cell"><span class="acf-label">' . $field['label'] . '</span></td><td class="tb-cell"><span class="acf-value">' . $value . '</span></td></tr>';
}
}
}
}
}
$out .= '</tbody></table></div>';
return $out;
}
}
CUSTOM_SHORTCODES::register_shortcodes();
In this instance, the demo shortcode is [custom_specs]
In our foreach loop, we can check to see if a field has value. If it has no value i.e. null then the field does not show in our for each loop.
if ( !empty($value) ) {
OR
https://www.advancedcustomfields.com/resources/hiding-empty-fields/
If the returned value is an array, we can convert the array to a comma list.
if(is_array($value)) {
$value = @implode(', ', $value);
}
Using this method, we can segment our ACF data into different foreach loop shortcodes for our use case scenario.
Specifications = [custom_specs]
Standard Features = [custom_feat]
Options = [custom_opt]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2 – Omit / Remove ACF field from the foreach loop.
The ‘price’ is recorded as an ACF field in our field group ‘specifications’.
However, the client does not want to show the ‘price’ field on the table on the front end.
How do we remove the ACF field ‘price’ from our ‘specifications’ foreach loop?
OPTION 1 – “SIMPLE”
Move the ACF field ‘price’ out of the current field group ‘specifications’ so it’s not returned in the shortcode loop [custom_specs].
If OPTION 1 does not fit your requirements, then try this alternative.
OPTION 2 – “ACF continue”
ACF Support has come back with the following. Interestingly, there is no mention of this in the current ACF documentation.
if( have_rows('specifications')){
while(have_rows('specifications')){
the_row();
if( $subfields = get_row()) {
foreach ($subfields as $key => $value) {
// Omit 'price' sub field - enter the field key
if ($key == 'field_123456789') { //skip this!
continue;
}
Using the ACF field key and continue;
we can now remove any ACF field inside our foreach loop.
Fantastic!
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
3 – Removing Conditional Logic Parent > Child ACF fields from our foreach loop.
In this scenario, we have the PARENT FIELD1 and the CHILD FIELD2
The fields can be any ACF field Type: Select, Checkbox, Radio Button, Button Group.
PARENT FIELD1 (*Required? = Yes)
( ) ABC
( ) MNO
( ) XYZ > Show CHILD FIELD2
CHILD FIELD2 (*Required? = Yes + Allow Null? = Yes )
( ) Choice 1
( ) Choice 2
( ) Choice 3
We have conditional logic CHILD FIELD2
Show this field if <PARENT FIELD1> Value is equal to <XYZ>
Great!
If PARENT FIELD1 (•) XYZ is selected, we expect CHILD FIELD2 Choices to show.
If PARENT FIELD1 ( ) XYZ is de-selected, we can expect FIELD2 to hide.
• • • • • • • • • • • • • • • • • • • • • •
Now, this is where it gets tricky.
As part of our strategy, we want to make it easy for the Client to update the data without making things worse.
The Client Selects <PARENT FIELD1>(•) XYZ
CHILD FIELD2 choices are shown.
All choices are null – they are empty.
The Client doesn’t make a selection from CHILD FIELD2 and updates the post.
We get a validation warning – CHILD FIELD2 is set to (*Required) so now we have to make a selection.
The Client selects CHILD FIELD2 (•) Choice 2
We validate, and the post is saved.
CHILD FIELD2 > Choice 2 is saved to the database.
We run our shortcode loop and CHILD FIELD2 is output into our table.
The Client decides, no, actually I didn’t want (•) XYZ, I really wanted ( ) ABC
The Client de-selects ( ) XYZ and selects (•) ABC
CHILD FIELD2 is hidden.
Even though <PARENT FIELD1>( )XYZ is not selected, our foreach loop is still outputting CHILD FIELD2 in the table.
Why ! – you ask?
In the database CHILD FIELD2 still has a saved value = Choice 2.
Our foreach loop condition is “only include fields that have value”.
So CHILD FIELD2 is still showing.
This stems from core ACF functionality – If the field is set to (*Required) ignore the null.
https://github.com/elliotcondon/acf/issues/591
How do we fix this?
• • • • • • • • • • • • • • • • • • • • • •
METHOD 1 – On all CHILD FIELD(s) disable (*Required)
(*Required? = No)
Each CHILD FIELD2 choice can be toggled ON or OFF. Allowing us to de-select our previous selected option. The post can be validated and saved without having a choice selected. The Database value is cleared on Post Update.
But wait – this now requires the Client to understand that the CHILD FIELD2 needs to be un-selected before unselecting the PARENT FIELD1.
LOL
Sometimes the simplest of workflow solutions are the hardest for clients to remember.
• • • • • • • • • • • • • • • • • • • • • •
METHOD 2 – JQuery
Use jQuery to null the CHILD FIELD when the PARENT FIELD is un-selected.
This improves our Method 1 workflow, as the Client doesn’t have to remember to unselect the CHILD FIELD2. Our jQuery Function will handle this.
For this to work, I changed my CHILD FIELD2 field Type to a ‘select’.
When the Field Type ‘select’ is set to (Allow Null? = Yes) a null choice is added to the top of the dropdown select “-Select-“.
https://www.advancedcustomfields.com/resources/select/
I used JQuery to select the first item on the CHILD FIELD2 ‘select’ list
when the Parent is de-selected.
In my WordPress Child theme functions.php
file I am loading a JS file when is_admin()
define( 'CHILD_THEM_URI', get_stylesheet_directory_uri() );
define( 'CHILD_THEM_DIR', get_stylesheet_directory() );
// register scripts for the wp_admin section
add_action( 'admin_head', 'register_admin_scripts' );
function register_admin_scripts() {
if ( is_admin() ) {
wp_enqueue_script( 'admin_js', CHILD_THEM_URI . '/includes/admin.js', array( 'jquery' ) );
}
}
In the JS File I have the following simple click function.
jQuery(document).ready (function ($) {
$('#parent_field_id').on('click',function () {
if ($(this).is(':checked')) {
//alert('You have Checked it');
} else {
//alert('You Un-Checked it');
// Set Conditional Select to null - First Item on the list
$("#child_field_id").prop("selectedIndex", 0);
}
});
});
• • • • • • • • • • • • • • • • • • • • • •
METHOD 3 – delete_field()
We could write a custom function if PARENT FIELD1 is de-selected then dynamically delete the conditional logic CHILD FIELD2 value from the database. Seems heavy to me!
delete_field()
delete_sub_field()
Someone with better PHP skills than me may wish to provide a solution.
I look forward to your responses.
• • • • • • • • • • • • • • • • • • • • • •
METHOD 4 – FUNCTION REQUEST
Have an additional toggle option on the ACF CHILD FIELD next to the (AllowNull? = Yes) to turn ON or OFF the required functionality.
If conditional logic PARENT Field is un-selected – return CHILD FIELD VALUES to the default Null State.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I think thats covers everything for now.
Hopefully this helps someone else in the future
Hi,
Another solution to this could be using an ACF addon that adds a new type of Google Map field allowing you to add more than one marker to the Map field. That way, instead of loading 1 map field for every marker, you could have 1 map field for theoretically as many markers as you need. That should not only make your admin page load a lot quicker but also make your markers a lot more manageable.
https://wordpress.org/plugins/acf-google-map-field-multiple-markers
I know I’m a bit late to this thread but I hope this helps someone 🙂
Are you trying to get this when the form is shown or during validation?
If it is in validation (based on the title of the OP) then there will be no way to get the post ID of the page the form is embedded on unless you provide that information.
There are a couple of methods you can use to do this.
1) Probably the method I would use would be to use either the html_before_fields or html_after_fields argument of wp_form() to add html that includes a hidden field in the form and populate that hidden field with the get_queried_object_id();
2) Create a field in acf for the current post ID. Use an acf/prepare_field filter to populate the field with the current post ID and to hide the field.
Hi Dorn
Thanks for the question and follow up answer
I have a similar problem where I am trying to get the ID of the page containing the form. However, the form creates new posts so the post_id returns as 0. Has anyone found a way of retrieving.
I have tried
$post_id = $_POST['post_id'];
$url = 'https://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
$current_post_id = url_to_postid( $url );
get_queried_object_id()
global $post;
$post_id = $post->ID;
etc...
I have also tried adding additional parameters to the function but alas....
All return 0. Is what I’m attempting actually possible?
Thanks
Okay, I think I managed it this way:
add_action( 'gform_pre_submission_1', 'gf_override_field_with_choice_label' );
function gf_override_field_with_choice_label( $form ) {
$post_id = get_query_var('course');
$startdate = rgpost( 'input_38' );
$first_row_value = get_post_meta($post_id, 'course_info_0_startdate', true);
$second_row_value = get_post_meta($post_id, 'course_info_1_startdate', true);
if ($startdate == $first_row_value) {
$_POST['input_40'] = get_post_meta($post_id, 'course_info_0_location', true);
} else if ($startdate == $second_row_value {
$_POST['input_40'] = get_post_meta($post_id, 'course_info_1_location', true);
} else {
$_POST['input_42'] = 'no value';
}
}
Don’t know whether there would be an easier/cleaner way? Or is this the right way?
Thanks for your input. I also read the documentation on https://www.advancedcustomfields.com/resources/repeater/ and try to match the subfield values with the code below, but still doesn’t work out.
add_action( 'gform_pre_submission_1', 'gf_get_corresponding_course_enddate' );
function gf_get_corresponding_course_enddate( $form ) {
$post_id = get_query_var('course'); //post id from url parameter
$startdate = rgpost( 'input_34' ); //value from Gravity forms field with id=34
$rows = get_field('course_info'); //repeater field is called course_info
if( $rows ) {
$first_row = $rows[0];
$first_row_title = $first_row['startdate']; // repeater subfield is called startdate
$second_row = $rows[1];
$second_row_title = $second_row['startdate'];
}
if ($first_row_title == $startdate) { // if gravity forms field input value equals startdate subfield value of the first row
$_POST['input_40'] = get_post_meta($post_id, 'stap_0_location', true); // add location subfield value of the first row to the gravity forms text field with id=40
} else if ($second_row_title == $startdate) {
$_POST['input_40'] = get_post_meta($post_id, 'stap_1_location', true);
// add location subfield value of the second row to the gravity forms text field with id=40
} else {
$_POST['input_40'] = 'location not known';
}
}
Unfortunately every time the result is ‘location not known’, even if the Gravity form input value equals the startdate subfield value…
Hi,
I have some issue regarding sorting using ACF field
In Woocommerce product I have added one ACF group named product_shipping_and_installment
and inside this group one field named product_type
in this field user will add “AC” Or “CO”.
Below is the code:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => "publish",
'product_cat' => $cat->name,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_shipping_and_installment_product_type',
'value' => array('AC', 'CO'),
'compare' => 'IN'
),
),
);
$loop = new WP_Query($args);
Above screenshot code will provide me all the list of product based on field named product_type
but not sorting wise. I would like to show “AC” value product list first and then need to show “CO” product list currently is showing me “CO” product list first and then “AC” product list.
Any help is greatly appreciate.
Thanks 🙂
Thanks for your help. I fixed the code as you suggested, but the same error shows:
Warning: Cannot modify header information – headers already sent by (output started at /home/customer/www/bermelloajamil.com/public_html/wp-content/themes/bifrost/header.php:1) in /home/customer/www/bermelloajamil.com/public_html/wp-includes/pluggable.php on line 1421
Here is a snippet of the fixed code:
<?php
if (get_field(‘general_redirect’, $bifrost_queried_object) && get_field(‘general_redirect_url’, $bifrost_queried_object)) {
wp_redirect(get_field(‘general_redirect_url’, $bifrost_queried_object));
exit;
}
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset=”<?php bloginfo(‘charset’); ?>”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
<?php
/**
* Queried Object
*
* In case it is shop page get_queried_object won’t
* work, it needs to be changed to a custom WooCommerce
* function wc_get_page_id.
*/
$bifrost_queried_object = class_exists(‘WooCommerce’) && is_shop() ? wc_get_page_id(‘shop’) : get_queried_object();
wp_head();
?>
</head>
Thanks so much for your help!
The problem is here
/**
* Redirect
*/
if (get_field(‘general_redirect’, $bifrost_queried_object) && get_field(‘general_redirect_url’, $bifrost_queried_object)) {
wp_redirect(get_field(‘general_redirect_url’, $bifrost_queried_object));
exit;
}
You are attempting to redirect after HTML output has been started. This code needs to happen before
<!DOCTYPE html>
Hi,
I have some similar issue
In Woocommerce product I have added one ACF group named product_shipping_and_installment
and inside this group one field named product_type
in this field user will add “AC” Or “CO”.
Below is the code:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => "publish",
'product_cat' => $cat->name,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_shipping_and_installment_product_type',
'value' => array('AC', 'CO'),
'compare' => 'IN'
),
),
);
$loop = new WP_Query($args);
Above screenshot code will provide me all the list of product based on field named product_type
but not sorting wise. I would like to show “AC” value product list first and then need to show “CO” product list currently is showing me “CO” product list first and then “AC” product list.
Any help is greatly appreciate.
Thanks 🙂
The issue issue is that that when editing there are no controls on a flex field other than “add new”. Adding an input here would be like adding an input to a text field or a radio field for the user to enter data into. This is not really possible for any type of field.
But if you want to make a request you can contact the devs here https://www.advancedcustomfields.com/contact/.
<?php if( $button_buy ):
$button_buy_url = $button_buy['url'];
$button_buy_title = $button_buy['title'];
$button_buy_target = $button_buy['target'] ? $button_buy['target'] : '_self';
?>
<a class="button button-buy" href="<?php echo esc_url( $button_buy_url ); ?>" target="<?php echo esc_attr( $button_buy_target ); ?>"><?php echo strip_tags($button_buy_title); ?></a>
<?php endif; ?>
Hey John, hope this helps. This ist the code from the template. I’m using normal img tag inside the Linktext. Can you see it in the picture i loaded up? I blacked out the URL for reasons.
Got it – thanks for the quick response.
How can I submit a feature request for something like this?
Okay I have the 404 page set within settings > reading, and I think I’m like halfway to getting it working on the frontend. I have this code in the 404.php template:
<?php
$post_id = intval(get_option('error_page'));
if (!empty($post_id)) {
$post = get_post($post_id);
setup_postdata($post);
get_template_part('page'); // this is the part where I think I have it wrong?
exit;
}
?>
The normal template that calls all of my flexible content rows is page.php so that’s why I set that in the line for get_template_part. However, it only reads the container div with empty content inside. (This code works on every other page just fine.) I know that just page.php isn’t technically part of a template, so maybe that’s why it doesn’t work? This is the content for that:
<div id="page__content">
<?php
$sectioncount = 0;
while ( have_posts() ) :
the_post();
// are there any rows within within our flexible content?
if( have_rows('page_content', $id )):
$sectioncount;
// loop through all the rows of flexible content
while ( have_rows('page_content', $id )) : the_row();
$sectioncount++;
?>
<?php get_template_part('template-parts/section', 'open'); ?>
<?php get_template_part('partials/page-content/'. get_row_layout()); ?>
<?php get_template_part('template-parts/section', 'close'); ?>
<?php
endwhile; // close the loop of flexible content
endif; // close flexible content conditional
endwhile; // End of the loop.
?>
</div>
See image attached for how it shows up on frontend. I really appreciate your help!
There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.
Brilliant! I didn’t even think of that. I’ve got the correct page set as “404 Page” with that code. Now the question is: how do I call this page’s content to display on the actual 404 page in the 404.php template?
That would be a simple way to do it yes but you may have issues if someone modifies the post title.
A safer way to do this would be to create an option page or if you have a theme options page to add a field there that allows the user to select a page to use as the 404 page. Then check this value in the 404 template and use that page if set.
If you really want to get fancy, check this 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.