Home › Forums › Add-ons › Repeater Field › Repeater field in functions.php
I am trying to call a repeater field from and specific post in functions.php using $post->ID but is not working.
If i use for a basic field like this it works:
<?php
$value = get_field( "my_field", $post->ID );
echo $value;
?>
and for my repeater field i sue this but is NOT working:
<?php if( have_rows('attachment_field', $post->ID) ): ?>
<ul>
<?php while( have_rows('attachment_field', $post->ID) ): the_row();
$valueField = get_sub_field('attachment_name' );
?>
<li>
<p><?php echo $valueField; ?> </p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Any suggestions? thanks in advance.
Your functions.php
wont know what the value of $post
is by default, so ideally you’d want to put that code into a function which passes the post ID value into it so that you can use that value.
But why not just put this code directly into your template – single or page – and then you can be inside the loop where the post ID value will be known?
Yes, i`ve declared this into a function in functions.php, this is my entire code:
function asset_empty_indexes(){
$post = get_post($_POST['post_id']);
<?php // My post ID displays ?>
<div class="hentry post" id="entry-<?php echo $post->ID; ?>">
<?php // My post title displays ?>
<?php echo apply_filters('the_title', $post->post_title) ?>
<?php // My post content displays ?>
<?php echo apply_filters('the_content', $post->post_content); ?>
<?php // My basic field displays ?>
<?php
$value = get_field( "prueba", $post->ID );
echo $value;
?>
<?php // But my repeater field does not displays ?>
<?php if( have_rows('attachment_field', $post->ID) ): ?>
<ul>
<?php while( have_rows('attachment_field', $post->ID) ): the_row();
$valueField = get_sub_field('attachment_name' );
?>
<li>
<p><?php echo $valueField; ?> </p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
}
I´m, using it this way because i call this function with ajax to insert it in a wordpress post, all my fields show except repeater field.
and is $post->ID
echo’ing out the post ID correctly elsewhere? IE in the id="entry-<?php echo $post->ID; ?>"
part?
Yes, it works. This show my post id, for example:
id="entry-3453"
What do you see if you do print_r(have_rows('attachment_field', $post->ID))
?
@miguelaichon – that’s a key bit of information because that means that it’s not finding ‘attachment_field’ correctly.
So you’d need to check some elementary things: Are you testing this on a post that in fact has content in that sub field? Is it named correctly?
Also, if you are having to specify the post ID in all your other fields, perhaps you need to use it in the sub field too?… IE $valueField = get_sub_field('attachment_name', $post->ID);
Yes, i double checked and the field and named right and they haven content.
Notice that if i use <?php print_r(have_rows('attachment_field', $post->ID)) ?>
is not working when i call the post content with ajax but if check the post directly in the url it works and displays “1”.
Where you have $post = get_post($_POST['post_id']);
, add this right below: setup_postdata( $post );
Try that & lemme know?
I worked around this calling posts with ajax from an api of posts and custom fields.
came here for the same thing, for example:
this works:
while(have_rows($listaEventoskey,’option’)){
the_row();
$thisid = get_sub_field(‘eventid’);
echo “i have this: $thisid<br>”;
}
but, this won’t work:
function infoEvento() {
while(have_rows($listaEventoskey,’option’)){
the_row();
$thisid = get_sub_field(‘eventid’);
echo “tenho o id de $thisid<br>”;
}
}
infoEvento();
Hi, I’m having the same issue but with ‘option’. Inside functions.php I have
if (have_rows('groups', 'option')) :
$output .= "here";
while (have_rows('groups', 'option')) : the_row();
$output .= "in";
endwhile;
endif;
But it nothing shows, not ever “here”. I’ve tried printing out have_rows('groups', 'option')
and it shows 1.
Is this an ACF limitation and I better find other ways to solve my problem?
I could solve it because I was calling have_rows() OUTSIDE from ‘wp_loaded’ hook. I was located have_rows() INSIDE ‘wp_loaded’ hook and it worked for me.
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.