使用 wordpress $wpdb insdie 短代码显示 <table> 内数据库中的数据

下面的代码不带格式打印到一个块中。鉴于短代码已经运行 php,我是否正确指定了 html?感觉不太像,因为 HTML 部分似乎不适合我。它只是作为整个块输出。(见图)


   function trying_2() {

    '

    <table border="1">

    <tr>

     <th>name</th>

     <th>partysize</th>

     <th>phonenumber</th>

    </tr>';


   

 global $wpdb;


// sending query

 $result = $wpdb->get_results ("SELECT *  FROM table_name");

    foreach ( $result as $print )   {


      echo '<tr>';

      echo '<td>' . $print->name.'</td>';

      echo '<td>' . $print->partysize.'</td>';

      echo '<td>' . $print->phonenumber.'</td>';

      echo '<td>' . $print->emailaddress.'</td>';

        echo '<td>' . $print->Time_stamp.'</td>';

        echo '<td>' . $print->currentstatus.'</td>';

    '</tr>';

    }

 '</table>';

}


add_shortcode('tryin', 'trying_2');

https://img1.sycdn.imooc.com/65aa38d8000169f506480081.jpg

沧海一幻觉
浏览 70回答 1
1回答

九州编程

您没有正确格式化HTML/ table,因此为什么您在 WordPress 页面上将所有内容打印在一行中。您不需要分别回显每一个td。您只需在您的variableto be和 中包装一个定义,然后使用即可,并且只需将循环数据连接到该变量即可。echoedfunction只需将其粘贴code到您的活动主题functions.php文件中,然后[tryin]在页面中调用您的短代码即可。(代码经过测试并且有效)function trying_2() {&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $results = '<table border="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>partysize</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>phonenumber</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>emailaddress</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Time_stamp</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>currentstatus</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; <tbody>';&nbsp; &nbsp; &nbsp; &nbsp; // sending query&nbsp; &nbsp; &nbsp; &nbsp; $WPQuery = $wpdb->get_results ("SELECT * FROM table_name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $WPQuery as $print )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results .= "<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->name</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->partysize</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->phonenumber</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->emailaddress</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->Time_stamp</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$print->currentstatus</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results .= "</tbody>&nbsp; &nbsp; </table>";&nbsp; &nbsp; //Print results&nbsp; &nbsp; echo $results;}add_shortcode('tryin', 'trying_2');
打开App,查看更多内容
随时随地看视频慕课网APP