mysql - WordPress - 页面中的动态内容

标签 mysql wordpress

因此,我可以编辑模板文件,通过创建一个选择该模板的新页面来回显登录用户的数据库信息 ( 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/

相关文章:

mysql - 如何编写存储过程

javascript - 如何在 facebook Pixel javascript 代码中使用 WordPress 自定义插件选项的值

wordpress - 如何将 id 添加到可视化 Composer 中的元素

wordpress - 您无权访问此服务器上的/wp-admin/post.php。 WordPress的

php - 将数组更改为sql查询

php - 在 SQL 查询中插入非常长的字符串 - 错误

PHP mysql查询没有提供额外的输出

php - MySQL:排序依据不正确

Wordpress 自定义主题 - 在 footer.php 中调用时不显示菜单

php - Wordpress:使用函数 get_post_field() 获取帖子内容时,简码不起作用