我不完全确定这是否是你想要的,但是。。。下面的代码将注册一个短码,它将获得给定的行并输出该行的所有字段:
function shortcode_db_row_cb( $atts ) {
$atts = shortcode_atts( array(
\'id\' => false,
), $atts, \'db_row\' );
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare( "select * from {$wpdb->prefix}TABLE_NAME where id = %d", $atts[\'id\'] ), ARRAY_A );
$res = \'\';
if ( $row ) {
$res = \'<table>\';
$res .= \'<tr>\';
foreach ( $row as $k => $v ) $res .= \'<th>\' . esc_html($k) . \'</th>\';
$res .= \'</tr>\';
$res .= \'<tr>\';
foreach ( $row as $k => $v ) $res .= \'<to>\' . esc_html($v) . \'</td>\';
$res .= \'</tr>\';
$res .= \'</table>\';
return $res;
}
return \'no such row\';
}
add_shortcode( \'db_row\', \'shortcode_db_row_cb\' );