$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WordPress Shortcode show database row|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WordPress Shortcode show database row

matteradmin7PV0评论

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.

Share Improve this question edited Mar 3, 2019 at 6:04 Sebastián asked Mar 3, 2019 at 5:59 SebastiánSebastián 1113 bronze badges 2
  • Could you explain what do you mean by “like an array or something like that”? – Krzysiek Dróżdż Commented Mar 3, 2019 at 6:03
  • Sorry, I'm wrong, I've been thinking many things with this problem – Sebastián Commented Mar 3, 2019 at 6:05
Add a comment  | 

1 Answer 1

Reset to default 1

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' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far