Select plugin version:
Since version: v1.5.1
View a Table with PHP
Get a HTML Table
Display a table
the_table('field_name');
Retrieving a table as a variable
$variable = get_table('field_name');
// do something with $variable if you know what you are doing
Display a table from get_field() value
$table_data = get_field('field_name');
$table_html = get_table( $table_data );
// or
the_table( $table_data );
$value
can be formatted or unformatted table data.
Example with Repeater field
// Check rows exists.
if( have_rows('repeater_field_name') ):
// Loop through rows.
while( have_rows('repeater_field_name') ) : the_row();
// Retrieves a sub table HTML as a variable
$table_html = get_sub_table('table_field_name');
// Outputs a sub table HTML.
the_sub_table('table_field_name');
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
Example with Flexible Content field
// Check value exists.
if( have_rows('flexible_content_field_name') ):
// Loop through rows.
while ( have_rows('flexible_content_field_name') ) : the_row();
// Case: Paragraph layout.
if( get_row_layout() == 'layout_name' ):
// Retrieves a sub table HTML as a variable
$table_html = get_sub_table('table_field_name');
// Outputs a sub table HTML.
the_sub_table('table_field_name');
endif;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
Filters
The table HTML can be adjusted using the following filter.
acf_tablefield/get_table/after_table
This filter enables HTML after the table using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/after_table', function( $html, $param, $table_data ) {
return $html;
}, 10, 3 );
-
$html
HTML string of the previously added HTML.
-
$param['field_name']
-
$param['post_id']
-
$param['field']
The ACF field object
-
$table_data
The table data.
acf_tablefield/get_table/append_table
This filter enables HTML appending the table using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/append_table', function( $html, $param, $table_data ) {
return $html;
}, 10, 3 );
-
$html
HTML string of the previously added HTML.
-
$param['field_name']
-
$param['post_id']
-
$param['field']
The ACF field object
-
$table_data
The table data.
acf_tablefield/get_table/attr/caption
This filter enables filtering the caption HTML element attributes using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/attr/caption', function( $attr, $data ) {
$attr['class'][] = 'highlighted';
return $attr;
}, 10, 2 );
-
$attr
A key value pair array of the html element attributes.
-
$data['field']
ACF field oject
-
$data['selector']
Field name or key
-
$data['post_id']
The post ID
-
$data['item_data']
The data of the current table cell
-
$data['table_data']
The data of the table
Result
<caption class="highlighted"></caption>
acf_tablefield/get_table/attr/cell
This filter enables filtering table cells HTML element attributes using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/attr/cell', function( $attr, $data ) {
$attr['class'][] = 'highlighted';
return $attr;
}, 10, 2 );
-
$attr
A key value pair array of the html element attributes.
-
$data['field']
ACF field oject
-
$data['selector']
Field name or key
-
$data['post_id']
The post ID
-
$data['section_key']
The table section like thead, tbody or tfoot
-
$data['col_index']
The index of the cells column
-
$data['row_index']
The index of the cells row
-
$data['item_data']
The data of the current table cell
-
$data['table_data']
The data of the table
-
$data['manager_id']
The table manager id if set.
Result
<td class="highlighted"></td>
acf_tablefield/get_table/attr/row
This filter enables filtering table rows HTML element attributes using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/attr/row', function( $attr, $data ) {
$attr['class'][] = 'highlighted';
return $attr;
}, 10, 2 );
-
$attr
A key value pair array of the html element attributes.
-
$data['field']
ACF field oject
-
$data['selector']
Field name or key
-
$data['post_id']
The post ID
-
$data['section_key']
The table section like thead, tbody or tfoot
-
$data['col_index']
The index of the cells column
-
$data['row_index']
The index of the cells cow
-
$data['item_data']
The data of the current table cell
-
$data['table_data']
The data of the table
-
$data['manager_id']
The table manager id if set.
Result
<tr class="highlighted"></tr>
acf_tablefield/get_table/attr/section
This filter enables filtering table section HTML element (tbody, thead, tfoot
) attributes using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/attr/section', function( $attr, $data ) {
if( 'tbody' !== $data['section_key'] ) {
return $attr;
}
$attr['class'][] = 'highlighted';
return $attr;
}, 10, 2 );
-
$attr
A key value pair array of the html element attributes.
-
$data['field']
ACF field oject
-
$data['selector']
Field name or key
-
$data['post_id']
The post ID
-
$data['section_key']
The table section like thead, tbody or tfoot
-
$data['item_data']
The data of the current table cell
-
$data['table_data']
The data of the table
-
$data['manager_id']
The table manager id if set.
Result
<tbody class="highlighted"></tbody>
acf_tablefield/get_table/attr/table
This filter is used in get_table()
or other table HTML returning functions and methodes and enables to filter/add/remove attributes and their values of the <table>
element.
Parameters
add_filter( 'acf_tablefield/get_table/attr/table', function( $attr, $data ) {
$attr['border'] = '0';
$attr['class'][] = 'highlighted';
$attr['style'][] = 'width: 50%;';
return $attr;
}, 10, 2 );
-
$attr
A key value pair array of the html element attributes.
-
$data['field']
ACF field oject
-
$data['selector']
Field name or key
-
$data['post_id']
The post ID
-
$data['table_data']
The data of the table
-
$data['manager_id']
The table manager id if set.
Result
<table border="0" class="highlighted" style="width: 50%;"></table>
acf_tablefield/get_table/before_table
This filter enables HTML before the table using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/before_table', function( $html, $param, $table_data ) {
return $html;
}, 10, 3 );
-
$html
HTML string of the previously added HTML.
-
$param['field_name']
-
$param['post_id']
-
$param['field']
The ACF field object
-
$table_data
The table data.
acf_tablefield/get_table/cell_content
This filter enables changing the content of table cells HTML elements using get_table()
or other table HTML returning functions and methodes.
Parameters
add_filter( 'acf_tablefield/get_table/cell_content', function( $cell_content, $param ) {
return $cell_content;
}, 10, 2 );
-
$cell_content
The content of the cell.
-
$param['elem_slug']
-
$param['data_key']
-
$param['table_options']
-
$param['row_options']
-
$param['cell_options']
-
$param['row_index']
-
$param['col_index']
acf_tablefield/get_table/prepend_table
This filter enables HTML prepending the table using get_table()
or other table HTML returning functions and methodes. This filter is for example used to insert the table caption.
Parameters
add_filter( 'acf_tablefield/get_table/prepend_table', function( $html, $param, $table_data ) {
return $html;
}, 10, 3 );
-
$html
HTML string of the previously added HTML.
-
$param['field_name']
-
$param['post_id']
-
$param['field']
The ACF field object
-
$table_data
The table data.