have_rows

The Advanced Custom Fields have rows function.

Description

have_rows( (string) $field_name, (bool) $post_id = false ); 

This function will instantiate a global variable containing the rows of a repeater or flexible content field, afterwhich, it will determine if another row exists to loop through

Parameters (2)

0. $field_name (string)
The field name.
1. $post_id — Optional. (bool) => false
The post id.

Usage

  1. if ( !function_exists( 'have_rows' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'advanced-custom-fields/core/api.php'; 
  3.  
  4. // The field name. 
  5. $field_name = ''; 
  6.  
  7. // The post id. 
  8. $post_id = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = have_rows($field_name, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/core/api.php  
  1. function have_rows( $field_name, $post_id = false ) { 
  2.  
  3. // vars 
  4. $depth = 0; 
  5. $row = array(); 
  6. $new_parent_loop = false; 
  7. $new_child_loop = false; 
  8.  
  9.  
  10. // reference 
  11. $_post_id = $post_id; 
  12.  
  13.  
  14. // filter post_id 
  15. $post_id = apply_filters('acf/get_post_id', $post_id ); 
  16.  
  17.  
  18. // empty? 
  19. if( empty($GLOBALS['acf_field']) ) 
  20. // reset 
  21. reset_rows( true ); 
  22.  
  23.  
  24. // create a new loop 
  25. $new_parent_loop = true; 
  26. else 
  27. // vars 
  28. $row = end( $GLOBALS['acf_field'] ); 
  29. $prev = prev( $GLOBALS['acf_field'] ); 
  30.  
  31.  
  32. // If post_id has changed, this is most likely an archive loop 
  33. if( $post_id != $row['post_id'] ) 
  34. if( $prev && $prev['post_id'] == $post_id ) 
  35. // case: Change in $post_id was due to a nested loop ending 
  36. // action: move up one level through the loops 
  37. elseif( empty($_post_id) && isset($row['value'][ $row['i'] ][ $field_name ]) ) 
  38. // case: Change in $post_id was due to this being a nested loop and not specifying the $post_id 
  39. // action: move down one level into a new loop 
  40. $new_child_loop = true; 
  41. else 
  42. // case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects 
  43. // action: leave this current loop alone and create a new parent loop 
  44. $new_parent_loop = true; 
  45. elseif( $field_name != $row['name'] ) 
  46. if( $prev && $prev['name'] == $field_name && $prev['post_id'] == $post_id ) 
  47. // case: Change in $field_name was due to a nested loop ending 
  48. // action: move up one level through the loops 
  49. elseif( isset($row['value'][ $row['i'] ][ $field_name ]) ) 
  50. // case: Change in $field_name was due to this being a nested loop 
  51. // action: move down one level into a new loop 
  52. $new_child_loop = true; 
  53.  
  54. else 
  55. // case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post 
  56. // action: leave this current loop alone and create a new parent loop 
  57. $new_parent_loop = true; 
  58.  
  59.  
  60.  
  61.  
  62. if( $new_parent_loop ) 
  63. // vars 
  64. $f = get_field_object( $field_name, $post_id ); 
  65. $v = $f['value']; 
  66. unset( $f['value'] ); 
  67.  
  68.  
  69. // add row 
  70. $GLOBALS['acf_field'][] = array( 
  71. 'name' => $field_name,  
  72. 'value' => $v,  
  73. 'field' => $f,  
  74. 'i' => -1,  
  75. 'post_id' => $post_id,  
  76. ); 
  77.  
  78. elseif( $new_child_loop ) 
  79. // vars 
  80. $f = acf_get_child_field_from_parent_field( $field_name, $row['field'] ); 
  81. $v = $row['value'][ $row['i'] ][ $field_name ]; 
  82.  
  83. $GLOBALS['acf_field'][] = array( 
  84. 'name' => $field_name,  
  85. 'value' => $v,  
  86. 'field' => $f,  
  87. 'i' => -1,  
  88. 'post_id' => $post_id,  
  89. ); 
  90.  
  91. }  
  92.  
  93.  
  94. // update vars 
  95. $row = end( $GLOBALS['acf_field'] ); 
  96.  
  97.  
  98. if( is_array($row['value']) && array_key_exists( $row['i']+1, $row['value'] ) ) 
  99. // next row exists 
  100. return true; 
  101.  
  102.  
  103. // no next row! 
  104.  
  105.  
  106. // return 
  107. return false; 
  108.