Support

Account

Home Forums Add-ons Repeater Field Repeater field in functions.php

Solving

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.

  • HI @miguelaichon

    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)) ?

  • Nothing, just empty space.

  • @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?

  • That also result in a empty space.

  • This reply has been marked as private.
  • This reply has been marked as private.
  • I worked around this calling posts with ajax from an api of posts and custom fields.

  • i have problem similar

  • 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?

  • Hello. I have same issue. Please, any solution?

  • I have the same Issue with AJAX. How did you fix it?

  • 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.

Viewing 20 posts - 1 through 20 (of 20 total)

The topic ‘Repeater field in functions.php’ is closed to new replies.