Select plugin version:
Since version: v1.5.1
the_table()
Displays an HTML table.
Parameters
the_table( $selector, [$post_id = false], [$escape_html = false] );
-
$selector
The field name, field key, or field value returned by the following:
get_field()
(formatted or unformatted)get_sub_field()
(formatted or unformatted)get_post_meta()
-
$post_id
-
Defaults:
false
The post ID where the value is saved. Defaults to
false
, which uses the current post.-
This example shows a variety of
$post_id
values for post, user, term and option.$post_id = false; // current post $post_id = 1; // post ID = 1 $post_id = "user_2"; // user ID = 2 $post_id = "category_3"; // category term ID = 3 $post_id = "event_4"; // event (custom taxonomy) term ID = 4 $post_id = "option"; // options page $post_id = "options"; // same as above
-
$escape_html
-
Defaults:
false
Displays an escaped HTML safe version of the table when
true
. Uses the'acf'
context for the'wp_kses_allowed_html'
filter. Refer to the ACF documentation for guidance on customizing escaped HTML.
Return
Displayd an HTML table.
Example with field name
the_table( 'my_field_name' );
Example with field key
the_table( 'field_664dcdebd4985' );
Example with field data
$table_data = get_field( 'my_field_name', $post_id );
// Do something with the table data if you know what you are doing
the_table( $table_data );
If you're working with table data and need an safe way to modify it, table_manager()
would be the preferred approach.
Example with Repeater
// 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
// 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
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.