我的数据库中有一个名为:persona的表.我只需要将该表中的数据检索到wordpress页面内的html表中.到目前为止,这就是我所拥有的:table border=1trthFirstname/ththLastname/ththPoints/th/tr...
我的数据库中有一个名为:persona的表.
我只需要将该表中的数据检索到wordpress页面内的html表中.到目前为止,这就是我所拥有的:
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM persona" );
foreach ( $result as $print ) {
echo '<td>' $print->ID_per.'</td>';
}
?>
</tr>
我在正在处理的特定页面中添加并发布它,但是当我刷新页面时,它仅显示页面中打印的代码.我想知道我是否将代码放在正确的位置,或者我不知道将代码放在哪里.
看下面的图片:
解决方法:
根据您的情况,最简单,最好的方法是在您的主题中添加一个shortcode.
如果将此代码添加到主题的functions.php文件中,则可以通过在任何页面或帖子中添加[角色表]来在任意位置显示信息.
// add the shortcode [persona-table], tell WP which function to call
add_shortcode( 'persona-table', 'persona_table_shortcode' );
// this function generates the shortcode output
function persona_table_shortcode( $args ) {
global $wpdb;
// Shortcodes RETURN content, so store in a variable to return
$content = '<table>';
$content .= '</tr><th>Firstname</th><th>Lastname</th><th>Points</th></tr>';
$results = $wpdb->get_results( ' SELECT * FROM persona' );
foreach ( $results AS $row ) {
$content = '<tr>';
// Modify these to match the database structure
$content .= '<td>' . $row->firstname . '</td>';
$content .= '<td>' . $row->lastname . '</td>';
$content .= '<td>' . $row->ID_per . '</td>';
$content .= '</tr>';
}
$content .= '</table>';
// return the table
return $content;
}
织梦狗教程
本文标题为:php-将数据库信息显示到html表WordPress中
基础教程推荐
猜你喜欢
- pbootcms文章插入图片不固定宽高的办法 2023-07-08
- 织梦dedecms后台添加变量提示Request var not allow 2023-07-08
- PbootCMS调用全站所有栏目最新文章的写法 2023-07-09
- PbootCMS的阿里云短信发送类 2023-07-09
- 织梦dedecms不显示未审核tag标签文档的的方法 2022-11-04
- Thinkphp5结合layer弹窗定制操作结果页面 2022-08-16
- dedecms织梦全局变量调用方法总结 2023-07-08
- dedecms织梦复制https链接网站图片无法本地化的解决办法 2022-06-23
- 织梦采集标题不完整的解决方法,修改标题长度 2022-07-14
- PbootCMS伪静态配置教程以及各web容器配置规则 2023-07-08