在wordpress之外获得自定义尺寸的精选图片

时间:2017-03-29 07:50:01

标签: php mysql wordpress wordpress-theming

我有WordPress博客,它作为Web应用程序的子目录托管。 WordPress安装不受影响,并在主题级别进行修改。 Web应用程序主页需要显示最近的博客帖子。所以我有以下代码来显示最近的帖子:

    $array = array();

    $query = "SELECT * FROM wp_posts WHERE post_status=:publish AND post_type=:post_type";
    $stmt = $this->dbj->prepare($query);
    $stmt->execute(array(
        ':publish' => 'publish',
        ':post_type' => 'post'
    ));

    if ($stmt->rowCount() > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $id = $row["ID"];
            if ($row["post_name"] == '') {
                $slug = $row["ID"];
            } else {
                $slug = $row["post_name"];
            }
            $content1 = strip_tags($row["post_content"]);

            $image_prepare = $this->dbj->prepare('SELECT
    wp_postmeta.meta_id,
    wp_postmeta.post_id,
    wp_postmeta.meta_key,
    wp_postmeta.meta_value
FROM
    wp_postmeta
INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE
    wp_postmeta.meta_key = :meta_key
AND wp_posts.post_parent = :pid');

            $image_prepare->execute(
                array(
                    ':pid' => $id,
                    ':meta_key' => '_wp_attached_file'
                )
            );

            $image_fetch = $image_prepare->fetchAll();

            foreach ($image_fetch as $image_row) {
                $image_name = $row['post_title'];
                $image_path = $image_row['meta_value'];
            }

            if (strlen($content1) > 200) {
                $content = substr($content1, 0, 200) . '...';
            } else {
                $content = nl2br($content1);
            }

            $array[] = array(
                'image_path' => $image_path,
                'image_name' => $image_name,
                'slug' => $row["post_name"],
                'content' => $content,
                'title' => $row['post_title']
            );
        }
    return $array;
    }

我正在获取完整尺寸的图片网址。我已在主题中的functions.php文件中调整了大小:

add_theme_support('post-thumbnails');

add_image_size('home-size', 214, 300);
add_image_size('home-featured', 300, 300);
add_image_size('jobs-thumb', 50, 75);

问题是上传目录中已调整大小的图片并不总是image-name-300x300.jpg home-featured。相反,它具有高度随机值,具体取决于文件名中图像的比例(例如:image-name-300x220.jpg)。我再次重新生成缩略图,但仍然是相同的文件名。如何获得'home-featured'上述解决方案?

1 个答案:

答案 0 :(得分:1)

Wordpress将生成的图像的序列化数据保存在postmeta表中,_wp_attachment_metadata作为meta_key。

在您的情况下,如果您的代码不需要完整尺寸的图片,请将_wp_attached_file更改为_wp_attachment_metadata

$image_prepare->execute(
    array(
        ':pid' => $id,
        ':meta_key' => '_wp_attached_file'
    )
);

$image_fetch = $image_prepare->fetchAll();

foreach ($image_fetch as $image_row) {
    $image_name = $row['post_title'];
    $image_meta = unserialize($image_row['meta_value']);
    $image_path = $image_meta['sizes']['home-featured']['file'];
}

$image_meta将类似于:

array (
    'width' => 720,
    'height' => 960,
    'file' => '2017/03/image.jpg',
    'sizes' =>
        array (
            'thumbnail' =>
                array (
                    'file' => 'image-150x150.jpg',
                    'width' => 150,
                    'height' => 150,
                    'mime-type' => 'image/jpeg',
                ),
            'medium' =>
                array (
                    'file' => 'image-225x300.jpg',
                    'width' => 225,
                    'height' => 300,
                    'mime-type' => 'image/jpeg',
                ),
            'twentyseventeen-thumbnail-avatar' =>
                array (
                    'file' => 'image-100x100.jpg',
                    'width' => 100,
                    'height' => 100,
                    'mime-type' => 'image/jpeg',
                ),
        ),