因此,我可以编辑模板文件,通过创建一个选择该模板的新页面来回显登录用户的数据库信息 ( http://www.makeuseof.com/tag/working-custom-database-tables-wordpress/ )。
我有一个包含内容的页面,其中包含一个 div (id="database")。我想将一些数据库内容回显到所选的 div 中。我在模板文件中使用它来在主页内容之后执行此操作:
<?php
if (is_user_logged_in())
{
global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM wps6_comments;");
print_r($customers);
}
?>
我不确定的是如何将此内容插入到页面内容本身(即 WordPress 内容)中,而不是插入到页面内容之后。有什么想法吗?
最佳答案
啊,WP 循环。好的。
您遇到的问题是您需要设置帖子的上下文。 (侧面不是。不要使用 $wpdb...在这样的文件中编写 SQL 语句不是一个好习惯。唉,你这样做了,我将在我的示例中使用它)。
<?php
// Setup and Query
global $wpdb;
$query = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'";
$result = $wpdb->get_results($query);
// Iterate through
foreach($result as $post):
// Set Post Context
setup_postdata($post);?>
<li><a href="<?php the_permalink()?>"><?php the_title();?></a></li><?php
wp_reset_postdata();
endforeach;?>
</ul>
我相信这就是您正在寻找的。请注意,我已经有一段时间没有使用普通的 php 模板了,而且我必须运行,所以其中肯定存在语法错误。
以下是一些文档链接: https://codex.wordpress.org/Function_Reference/setup_postdata 还有一些理论: https://developer.wordpress.org/themes/basics/the-loop/
关于mysql - WordPress - 页面中的动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45602120/