【问题标题】:How can I retrieve posts with featured images from a WordPress database if WordPress is no longer installed? [closed]如果不再安装 WordPress,我如何从 WordPress 数据库中检索带有特色图片的帖子? [关闭]
【发布时间】:2015-11-05 05:34:55
【问题描述】:

我在 MySQL 中有一个 WordPress 数据库,但不再安装 WordPress。如何仅使用 PHP 和 MySQL 检索帖子及其特色图片?

【问题讨论】:

  • @Suchit 使用的是 WP 代码,而不是普通的 php
  • 为什么需要自己编写代码?如果有一个现有的功能,你应该使用它。
  • 我不再在我的服务器上安装 WP,我只有 sql 表,其中包含我需要显示的大量内容/帖子
  • 如果你没有安装 WP,你确定特色图片还在吗?他们本来会在 wp-content/uploads 中,所以如果它指向 nowt,找到他们的参考可能是愚蠢的差事。

标签: php mysql wordpress


【解决方案1】:

您最好的选择是MySQLI

<?php

$wp_database = new mysqli("yourhost", "yourusername", "yourpasssword", "yourdatabase");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// Here's how you get all the posts. Adjust table names to suit.

$sql = "SELECT * FROM `wp_posts` WHERE `post_status` LIKE 'publish' AND `post_type` LIKE 'post'";

$result =  mysqli_query($wp_database, $sql);

$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
foreach ($posts as $post): ?>
<div>
    <?php print_r($post); ?>
</div>

<?php endforeach;

// And here's how you get an individual image url.

$post_id = 1181; // Your post ID here

$sql = "SELECT `guid` FROM `wp_posts` WHERE `id` IN (SELECT  `meta_value` 
FROM  `wp_postmeta` 
WHERE  `meta_key` LIKE  '_thumbnail_id'
AND  `post_id` = $post_id)";

$result = mysqli_query($wp_database, $sql);
$images = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
foreach ($images as $image): ?>
<div>
    <?php print_r($image); ?>
</div>

<?php endforeach; ?>

【讨论】:

  • 谢谢!耶!就是这个!它有效!!!
猜你喜欢
  • 2013-10-04
  • 2020-04-07
  • 2013-11-20
  • 2012-06-30
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
相关资源
最近更新 更多