Good day,
I was wondering if can do a shortcode that gets all the fields in a row stored in a database, with the difference that I do not want to do many shortcodes for the speed of the site.
The row of the database with separate fields, as you will see in the image below.
Or if there is another way to do it and it is better, you're welcome.
Good day,
I was wondering if can do a shortcode that gets all the fields in a row stored in a database, with the difference that I do not want to do many shortcodes for the speed of the site.
The row of the database with separate fields, as you will see in the image below.
Or if there is another way to do it and it is better, you're welcome.
I’m not entirely sure if that’s what you wanted, but... Here’s the code that will register a shortcode, which will get given row and output all the fields of it:
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' );