If I add images to the gallery field via admin panel (not directly to db), this parameter equals to “2” or “3” or “4” but why precisely?
This has been bugging me for quite a while, but I think I’ve finally cracked it. It was almost disappointingly simple in the end – $value
needs to be an array.
add_filter('acf/load_value/key=field_54aa5b3b008b0', 'afc_load_my_repeater_value', 10, 3);
function afc_load_my_repeater_value($value, $post_id, $field) {
if ($post_id === false) {
$value = array();
$value[] = array(
field_54aa5beb008b1 => 'Hours',
field_54aa5c25008b2 => 1
);
$value[] = array(
field_54aa5beb008b1 => 'Minutes'
);
$value[] = array(
field_54aa5beb008b1 => 'Seconds'
);
}
return $value;
}
This case is pretty straight forward; I have one repeater field field_54aa5b3b008b0
that has two text fields inside it (field_54aa5beb008b1
and field_54aa5c25008b2
). I imagine the same setup will apply to more in-depth scenarios as well with some tweaking.
Step 1. Hook into acf/load_value
on your repeater field. You can use type/key/name as desired here.
Step 2. Check if the $post_id
is set or not. If you want to modify the values of existing posts as well you wouldn’t need to, but here I want to set default values for new posts only.
Step 3. Set $value
to your array of default values. Above, I have 3 rows by default on new posts. Each row has two values – ‘unit of time’ and ‘amount of time’. The ‘unit of time’ values are set to “Hours”, “Minutes” and “Seconds”. The ‘amount of time’ value on the “Hours” row I have set default to 1. In the other rows, I didn’t want the second value to be set by default, so it’s omitted – if omitted, ACF uses the default value set in the admin (or just blank).
Step 4. Return the $value
. Make sure you always return it – even if you haven’t modified it.
———– NOTES
– I’m using ACF Pro 5.1.8, but it should work in other versions as well (using the proper hooks as needed).
– The sub fields appear to be required to the the exact field key – field name and type haven’t worked for me.
– Above, I’m setting a static set of values as my defaults. You could certainly call a function or otherwise dynamically load an array into $value
from elsewhere as well – just need to make sure it uses the key for sub fields.
– If you’re not sure how the array should be set up, just print out an array from an existing repeater in the wp-admin like this:
add_filter('acf/load_value/key=field_54aa5b3b008b0', 'afc_load_my_repeater_value', 10, 3);
function afc_load_my_repeater_value($value, $post_id, $field) {
echo print_r($value);
return $value;
}
This will print out the repeater value from an existing post. Save the post with some rows in your repeater and you’ll see a guide of how to set up the array of defaults.
I understand it has to be looped through slb_activate()
but how? It’s not explained anywhere to dummies like me.
I have my gallery displaying using the following code, but the lightbox doesn’t do a thing.
$images = get_sub_field('sc_images');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This is an old post, but I came across exactly the same problem with ACF 4.4.0. I have two relationship fields assigned to my normal WP posts.
One of the relationship fields displays a list of all WP posts and has no taxonomy restrictions. The posts appear as expected in the relationship selection area.
The other relationship field is supposed to display a list of custom post types restricted by a custom taxonomy. Nothing appears in this relationship selection area; it’s completely blank. The field itself appears, but the left pane is empty. Removing the taxonomy restriction did not fix the problem – the only way I could get anything to appear in the left pane was to choose either WP posts or WP pages as the post type.
This appears to be a bug, but considering the lack of similar reports (I have been unable to find much online), maybe it’s a unique situation.
I upgraded to 5.0 and that fixed the problem – kind of. The left pane correctly populates with the custom post type, but the search feature does not work. Regardless of what I enter, I get “no matches found”. I describe this problem in this related ticket:
So, upgrading to ACF 5.0 fixes the issue of the field not being populated, but now search does not work with custom post types.
i would recommend do something like that:
<?php
$images = get_field('gallery');
$img_count=1;
if( $images ): ?>
<?php foreach( $images as $image ):
if ($img_count == 1) { ?>
<div class="large">
<a rel="gallery" href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
</div>
<?php } else {?>
<div class="thumb">
<a rel="gallery" href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
</div>
<?php }
$img_count++; ?>
<?php endforeach; ?>
<?php endif; ?>
instead of trying to use css only.
you probably need to add a popup-script like fancybox too, to get a result like your example.
(with help of css3, it should be possible for new browsers, to do a css only solution. look at :first-of-type for example, but i would not recommend that)
sure you can have repeater inside flexible field.
from original docs here
Nested Loop
<?php
// check if the flexible content field has rows of data
if( have_rows('flexible_content_field_name') ):
// loop through the rows of data
while ( have_rows('flexible_content_field_name') ) : the_row();
// check current row layout
if( get_row_layout() == 'gallery' ):
// check if the nested repeater field has rows of data
if( have_rows('images') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
echo '<li><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></li>';
endwhile;
echo '</ul>';
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
basically it is nothing very special. it is just a (repeater) loop inside a (flexible content) loop
@paaljoachim if you don’t open <article> remove the closing </article>, and remove a closing </div> too. it looks like my original code has also one to much.
replace </div></div></article>
with </div>
else it looks fine
or add something like that <article itemtype="http://schema.org/CreativeWork" itemscope="itemscope" class="post-<?php print $pageid; ?> page type-page status-publish entry"><div class="entry-content" itemprop="text">
above <div class="movie-content">
it depends if you have a post for every user or not.
if yes, than it may work with this shortcode [acf field="sms2" post_id="user_1"]
of course only for user 1
if you wish to have 1 post and show value depends on user, i think, you need a custom template for that post
I figured out a work around
$feature is the parent repeater field.
<?php $media = $feature['feature_media']; if($media): ?>
<div id="feature_media">
<?php
foreach($media as $key=>$row) {
$layout = $row['acf_fc_layout'];
if($layout == 'static_image'):
$image = $row['image'];
echo '<img src="'.$image['url'].'">';
elseif($layout == 'slideshow'):
$slides = $row['images'];
if($slides):
echo '<ul class="slides">';
foreach($slides as $slide) {
echo '<img src="'.$slide['url'].'">';
}
echo '</ul>';
endif;
elseif($layout == 'video'):
$embedcode = $row['embed_code'];
echo $embedcode;
endif;
} // end each row
?>
</div>
<?php endif; // end if media ?>
Not sure this is the right approach but at least it worked.
<?php
/*
Template Name: Form ADD
*/
acf_form_head();
get_header();
the_post();
?>
<div class="acfform">
<?php acf_form(array(
'field_groups' => array(xxxxxxxxx),
'post_id' => 'xxxxx',
'submit_value' => 'ADD',
'return' => '%post_url%',
'updated_message' => ''
)); ?>
</div>
<?php
/*
Template Name: Form Edit
*/
acf_form_head();
get_header();
the_post();
?>
<div class="acfform">
<?php
$post_id = $_GET['post_id']; // get post_id option
$options = array(
'post_id'=> $post_id,
);
acf_form($options);
?>
<?php if ( is_user_logged_in() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php acf_form(array(
'field_groups' => array(xxxxxxxxxxx),
'post_id' => $post_id,
'post_title' => false,
'return' => '%post_url%',
'submit_value' => 'Atualizar Anuncio'
)); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
Thanks for the link, but what I meant was that a field would only be visible to users logged in as administrators, not that a field would appear on the administrator profile page.
I know that ACF doesn’t do view counting ;). It was a quick example to illustrate a use for an admin-only visibility field.
Ok, I might be wrong, but I don’t think the shortcode will let you retrieve values from a user. A shortcode only let you indicate field name and post_id.
But this might point you out in the right direction:
http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/
The documentation says:
All the API functions can be used with a user, however, a second parameter is required to target the user ID. This is similar to passing through a post_id to target a specific post object.
The $post_id needed is a string containing “user” + the user ID in this format: “user_$UserID”
You will need to specifiy the user_ID for that. But as I said before, I dont think that’s available in a shortcode.
One Question – why is the acf_form() two times in the editform.php template?
Somehow my forms are not saving (I am using the acf_form_head(); function)
May you please take a look to my screencast?
I may didn’t explain the problem correctly
My code has several levels. This code I sent you before is the code for projects-image.php (see get get_template_part below) and the loop for my posts is inside projects.php, from where I am calling different files with different ACF structure, like this:
<?php get_header(); ?>
<?php
/*
Template Name: Projects
*/
?>
<?php $query = new WP_Query(
array(
'posts_per_page' => 1,
'category_name' => 'projects',
'orderby' => 'date',
'order' => 'DESC'
));
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="row">
<article class="projects large-12 columns ">
<section class="large-6 large-centered columns">
<?php if( have_rows('projects_content') ):
while ( have_rows('projects_content') ) : the_row(); ?>
<?php get_template_part( 'project', 'image' ); ?>
<?php get_template_part( 'project', 'video' ); ?>
<?php get_template_part( 'project', 'text' ); ?>
<?php endwhile;?>
<?php endif; ?>
</section>
</article>
</div><!-- end.row -->
<?php // Copyright ?>
<div class="row">
<?php get_template_part('/parts/copyright'); ?>
</div>
<?php }
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();?>
<?php get_footer(); ?>
@hpctech the answer is simple: In your case the ACF is empty, because there is no place so far where you are filling the information that this field should be displaying. The ACF is a “container”, but from where is this container grabbing the information that should be displayed?
Let’s say you use a post to do this. If I am not wrong in your current settings, if you go to Posts > Add new, you will see the new sms2 field you created (below the content editor). Fill that text area with something, a number, whatever, and save it, then in the page where you are using the shortcode write [acf field="sms2"]
or [acf field="sms2" post_id="524"]
and replace 524 for the post-ID from where you want to grab this info.
Ok, now it’s clearer.
My question would be: if you just need to output text in a WYSIWYG field, why dont you just write the text in the_content() and attach the audio to that field as well? Why two fields?
But try this code and tell me if it works:
<div class="chapo">
<?php the_field('chapo'); ?>
</div>
<div class="content">
<?php the_content(); ?>
</div>
If it doesn’t, can you inspect the html with chrome or firefox and tell me what’s being displayed inside these divs?
1) what are you puting in the chapo field.
<p>WYSIWYG content from get field</p>
2) what are you putting in the content editor
<audio><!-- attachment audio stuff... --></audio>
(this really is the media file)
3) where / how is the attachment being added
From the media library (wp-admin/media-new.php)
4) whats the code in your php file
page.php
<?php
get_header();
while (have_posts())
{
the_post();
if ( get_post_type() == 'attachment' )
{
get_template_part( 'attachment', 'single' );
}
else
{
get_template_part( 'content', 'page' );
}
}
get_footer();
attachment-single.php
<div class="chapo">
<?php
if ( get_field( 'chapo' ) )
{ ?>
<?php echo get_field( 'chapo' ); ?>
<?php
} ?>
</div>
<div class="content">
<?php the_content(); ?>
</div>
5) what should be the desired output?
<div class="chapo"><p>WYSIWYG content from get field</p></div>
<div class="content"><audio><!-- attachment audio stuff... --></audio></div>
6. What actually appears?
<div class="chapo"><audio><!-- attachment audio stuff... --></audio><p>WYSIWYG content from get field</p></div>
<div class="content"><audio><!-- attachment audio stuff... --></audio></div>
Note: This happens with any media file, not just audio.
Bump. Custom fields stored as options are still generating multiple SQL queries per get_field call due to the lack of autoload which is very bad.
If a per-option setting isn’t possible, then the plugin should go back to defaulting to autoload.
Another person with the same request: http://support.advancedcustomfields.com/forums/topic/options-as-autoload-true/
what about using this: wp_update_post( $my_post );
i use them to update post_title or post_content with values that come from get_field
or the save action: (all you need is a hidden field with name “totalprice“)
function my_acf_update_totalprice($post_id)
{
$total = get_field('price') * get_field('qty');
$value = $total;
$field_name = "totalprice";
update_field($field_name, $value, $post_id);
}
add_action('save_post', 'my_acf_update_totalprice');
Hi thank you for the suggestion.
The issue is that on the php side while I’am saving the new value I need to get the values of other ACF fields, something like
$total = get_field(‘price’) * get_field(‘qty’);
update_field(‘total’, $total);
BUT the issue is that I cannot use get_field(‘xxxx’) as the field xxx is not yet saved in the DB (as is a new post).
//add title and alias when a person is created
function my_acf_update_titlevalue( $value, $post_id, $field ) {
global $post;
// vars (title comes from person_name field that is looked for at the filter, after that the alias is generated)
$new_title = $value;
$new_slug = sanitize_title( $new_title );
// update post with the "new" title and alias (because we may hide them at our custom post)
$my_post = array('ID'=> $post_id,'post_title' => $new_title,'post_name' => $new_slug );
wp_update_post( $my_post );
return $value;
}
add_filter('acf/update_value/name=person_name', 'my_acf_update_titlevalue', 10, 3);
try something like above at your plugin: change person_name to your acf field
hope that help and work for you
use a hidden field and update value of it based on the other values.
may use that: https://github.com/folbert/acf-hidden (out of this thread)
than use update_field or update_value to save it into DB
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.