Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Last active September 15, 2021 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarreros/750ee1aacbe7344a09c790ae92e859ac to your computer and use it in GitHub Desktop.
Save jmarreros/750ee1aacbe7344a09c790ae92e859ac to your computer and use it in GitHub Desktop.
<?php //obviar este línea
add_filter( 'the_content', 'dcms_list_data' );
function dcms_list_data( $content ) {
$slug_page = 'frutas'; //slug de la página en donde se mostrará la tabla
$table_name = 'data_fruit'; // nombre de la tabla
if (is_page($slug_page)){
global $wpdb;
$items = $wpdb->get_results("SELECT * FROM `$table_name`");
$result = '';
// nombre de los campos de la tabla
foreach ($items as $item) {
$result .= '<tr>
<td>'.$item->fruit_id.'</td>
<td>'.$item->name.'</td>
<td>'.$item->variety.'</td>
</tr>';
}
$template = '<table class="table-data">
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Variedad</th>
</tr>
{data}
</table>';
return $content.str_replace('{data}', $result, $template);
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment