使用 WordPress 从数据库读取

发布于 2024-12-04 14:57:11 字数 239 浏览 9 评论 0原文

我对 WordPress 很陌生,对 PHP 知之甚少。 我阅读了有关 PODS 的内容,并且知道如何创建 PODS 并使用页面/模板来显示数据。

我遇到的问题是,我创建的 PODS 使用通过 WP 仪表板输入的静态数据,我想要的是从数据库读取数据,我正在使用 MySql(与 WordPress 使用的数据库相同)。有没有办法使用 PODS 并从数据库读取数据,或者 wordpress 有更好的方法来处理来自数据库的数据?

谢谢

I am very new to WordPress, I have very little knowledge with PHP.
I read about PODS and I know how to create one and use pages / templates to display data.

The issue I am havingis, the PODS I was creating use static data entered via the WP dashboard, what I want is to read data from a database, I am using MySql (same DB that wordpress is using). is there a way to use PODS and read the data from the DB, or wordpress has a better way to handle data coming from the DB ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法言说的痛 2024-12-11 14:57:11

在 WordPress 中读取数据库的常用方法如下:

  1. 获取全局变量 $wpdb

    global $wpdb
  2. 准备输出和SQL命令

    <前>$输出=“”;
    $sql = "SELECT ".$wpdb->前缀."posts.post_title,
    “.$wpdb->前缀。”posts.post_name FROM “。
    $wpdb-> 前缀。"帖子 WHERE ".$wpdb-> 前缀。
    "posts.post_status='发布' AND ".$wpdb-> 前缀。
    "posts.post_parent=0 AND ".$wpdb-> 前缀。
    "posts.post_type='sometype'";

  3. 方法 get_results() 从数据库检索值

    $posts = $wpdb->get_results($sql);
    $输出.= '';
    foreach ($posts 作为 $post) {
    $output .= '
  4. 帖子名称)。 '">'.strip_tags($post->post_title).'
  5. '; } $输出.= ''; echo $output;

Usual way to read from database in WordPress is the following:

  1. get global variable $wpdb

    global $wpdb
  2. prepare the output and SQL command

    $output = "";
    $sql = "SELECT ".$wpdb->prefix."posts.post_title,
    ".$wpdb->prefix."posts.post_name FROM ".
    $wpdb->prefix."posts WHERE ".$wpdb->prefix.
    "posts.post_status='publish' AND ".$wpdb->prefix.
    "posts.post_parent=0 AND ".$wpdb->prefix.
    "posts.post_type='sometype'";
  3. method get_results() retrieves values from db

    $posts = $wpdb->get_results($sql);
    $output .= '';
    foreach ($posts as $post) {
    $output .= '
  4. post_name). '">'.strip_tags($post->post_title).'
  5. '; } $output .= ''; echo $output;
浅黛梨妆こ 2024-12-11 14:57:11

您应该查看 $wpdb 变量(和类)
http://codex.wordpress.org/Class_Reference/wpdb

请记住将其声明为全局的:

<?php global $wpdb; ?>

但是我不确定你想要什么。
我建议靠近 WordPress。
如果您想在不使用代码的情况下创建自己的自定义帖子类型,请使用 moretypes

You should Look into the $wpdb variable (and class)
http://codex.wordpress.org/Class_Reference/wpdb

Do remember to declare it a global:

<?php global $wpdb; ?>

I am however not sure what you want.
I advise staying close to wordpress.
If you want to create your own custom post types without using code use moretypes

(り薆情海 2024-12-11 14:57:11

Wordpress CSM 有一个非常好的与 db 一起使用的类,我认为更好的选择是学习 db 如何连接并从 mysql 获取数据

Wordpress CSM has a very good class to work with db, i think the better bet on this is learn how db connects and get data from mysql

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文