繁体   English   中英

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

[英]Display data from database inside <table> using wordpress $wpdb insdie a shortcode

下面的代码打印到一个块中,没有格式化。 鉴于短代码已经运行 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');

这个打印的图片

您没有正确格式化HTML / table ,因此为什么您在 wordpress 的页面上将所有内容打印在一行中。

并不需要相互呼应td分开。 您只需将一个variable定义为要echoed并在您的function使用,您只需要将循环数据连接到该变量。

只需将以下code粘贴到您的活动主题functions.php文件中,然后在页面中调用您的短代码[tryin] (代码测试和工作)

function trying_2() {

    global $wpdb;
    
    $results = '<table border="1">
            <thead>
                <tr>
                    <th>name</th>
                    <th>partysize</th>
                    <th>phonenumber</th>
                    <th>emailaddress</th>
                    <th>Time_stamp</th>
                    <th>currentstatus</th>
                </tr>
            </thead>
        <tbody>';

        // sending query
        $WPQuery = $wpdb->get_results ("SELECT * FROM table_name");
            foreach ( $WPQuery as $print )   {
                $results .= "<tr>
                            <td>$print->name</td>
                            <td>$print->partysize</td>
                            <td>$print->phonenumber</td>
                            <td>$print->emailaddress</td>
                            <td>$print->Time_stamp</td>
                            <td>$print->currentstatus</td>
                        </tr>";
                    }
                $results .= "</tbody>
    </table>";
    //Print results
    echo $results;
}

add_shortcode('tryin', 'trying_2');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM