修改@Kirill Fuchs的答案。如果在短代码上使用此代码,则可能会产生问题。它可能以错误的顺序显示输出。为了避免这种情况,我用return代替echo。尝试通过以下方式在短代码上执行此操作:
add_shortcode(\'custom_db\', function(){
global $wpdb;
$table_name = $wpdb->prefix . \'liveshoutbox\';
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($retrieve_data as $retrieved_data){
$f_name = $retrieved_data->column_name;
$f_text = $retrieved_data->another_column_name;
}
$output = \'<div class="wrap">
<h2>Table of clients.</h2>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr>
<td>\'. $f_name .\'</td>
<td>\'. $f_text .\'</td>
</tr>
</table>
</div>\';
return $output;
} );