【问题标题】:how to get all the posts that are posted on WordPress website using php如何使用 php 获取发布在 WordPress 网站上的所有帖子
【发布时间】:2018-05-19 15:21:49
【问题描述】:

我正在用 PHP 制作自己的应用程序,我想显示我在 WordPress 网站上发布的所有帖子(两者都在同一台服务器上)。我知道 WordPress 将数据存储在数据库中,但我无法找到帖子的所有部分。

所以我的问题是如何访问在我的 WordPress 网站上发布的每一篇文章和文章。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • @user3783243 没有找到任何东西
  • @PeterM 我试图在 wp_post、wp_terms、wp_terms_relation、wp_terms_taxonomy 中找到帖子数据...但我只能在 wp_terms 中找到帖子的标题和 slug
  • 数据库结构不一样?从该图中,您的数据应该在wp_posts 中。 wp_terms 是帖子表的两个级别。请添加您检索标题的方式以及您对posts 表的尝试。

标签: php mysql wordpress


【解决方案1】:

如果您使用 WordPress,只需使用原生 WordPress 函数,即 WP_Query 类。

$the_query = new WP_Query( ['posts_per_page' => -1]);

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        // All Post Data Available Now
        // Do what you want with it
    }
    wp_reset_postdata();
}

在 while 循环内,您可以使用标准 WP 函数(如 get_the_content()、get_the_title(),甚至 get_post_custom()(如果您有自定义字段))访问所有相关的帖子信息。

【讨论】:

  • 感谢它对我有用,我试图在不使用 wp 函数的情况下从纯 php 获取它,但现在我将使用它来代替
【解决方案2】:

为了访问所有 WordPress 功能,您需要将其添加到您的 php 文件中。

<?PHP

    // require wp-load.php to use built-in WordPress functions
    require_once("../wp-load.php");

?>

现在您所需要的只是,运用您的思想和 WordPress 技能,使用基本的 WP 功能获取帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多