Support

Account

Home Forums Add-ons Repeater Field Post Object in Repeater Field. Need Rows Count

Solving

Post Object in Repeater Field. Need Rows Count

  • I have a Repeater field (field name is “positions”) with a Post Object field (field name is “position”) as the field that repeats. I’d like to be able to count how many “position” the user has added and customize the front-end so that everything displays correctly.

    I found the following code at https://support.advancedcustomfields.com/forums/topic/if-repeater-field-has-1-rows-2-rows-etc/. It helped me, but $count is returning 1 when it should be 2.

    <?php $count = count( get_sub_field( 'position' ) );
    if ( $count == 1 ) { ?>
    <div class="full-width">
    <?php } elseif ( $count == 2 ) { ?>
    <div class="half-width">
    <?php } elseif ( $count >= 3 ) { ?>
    <div class="one-third">
    <?php } ?>

    Like I said, the code above is working, but $count is returning 1, not 2.

  • Hi @phej

    I believe you need to count the repeater field instead of the subfield like this:

    $count = count( get_field( 'positions' ) );

    I hope this helps 🙂

  • Hey @acf-support,
    I tried $count = count( get_field( 'positions' ) ); but <?php echo $count;?> still echos “1”

    Here’s the field in the admin: Here's the field in the admin and here it is on the page: here it is on the page. Not sure if that helps or not.

  • Hi @phej

    That’s weird. Could you var_dump() the returned data like this:

    echo <pre>;
    var_dump(get_field( 'positions' ));
    echo </pre>;

    Also, could you please try to add the post/page ID as the second parameter of get_field() like this:

    echo <pre>;
    var_dump(get_field( 'positions', 99 ));
    echo </pre>;

    Where ’99’ is the post/page ID that has the repeater field.

    Thanks 🙂

  • @acf-support,

    get_field( ‘positions’ ) yielded:

    array(2) {
      [0]=>
      array(1) {
        ["position"]=>
        object(WP_Post)#597 (24) {
          ["ID"]=>
          int(243)
          ["post_author"]=>
          string(1) "1"
          ["post_date"]=>
          string(19) "2016-06-17 14:35:53"
          ["post_date_gmt"]=>
          string(19) "2016-06-17 14:35:53"
          ["post_content"]=>
          string(1271) "

    get_field( ‘positions’, 10 ) yielded:

    array(2) {
      [0]=>
      array(1) {
        ["position"]=>
        object(WP_Post)#597 (24) {
          ["ID"]=>
          int(243)
          ["post_author"]=>
          string(1) "1"
          ["post_date"]=>
          string(19) "2016-06-17 14:35:53"
          ["post_date_gmt"]=>
          string(19) "2016-06-17 14:35:53"
          ["post_content"]=>
          string(1271) "

    As well as the content for both posts.

  • This reply has been marked as private.
  • Hi @phej

    That’s weird. As you can see, the var_dump() returns array(2), which mean the count() function should return “2” too. Could you please try it like this:

    echo count(get_field( 'positions' ));

    And:

    echo count(get_field( 'positions', 10 ));

    If it still returns ‘1’, could you please open a new ticket and provide temporary credentials to your site here: https://support.advancedcustomfields.com/new-ticket?

    Don’t forget to include the URL to this thread and let know where you can find the code in your template file.

    Thanks 🙂

  • Yeah, it is weird.

    I made a comment with the URL, but it’s private – not sure if you can see that or not – as the site is still in dev. I’ll try that tomorrow and open a ticket if it doesn’t work.

    Thanks!

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

The topic ‘Post Object in Repeater Field. Need Rows Count’ is closed to new replies.