簡體   English   中英

在Wordpress之外以自定義尺寸獲取特色圖片

[英]Get featured image with custom size outside wordpress

我有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);

問題是上載目錄中調整大小的圖像並不總是具有home-featured image-name-300x300.jpg 取而代之的是,它具有隨機的高度值,具體取決於文件名中圖像的比例(例如: image-name-300x220.jpg )。 我又重新生成了縮略圖,但文件名仍然相同。 如何獲得上述解決方案的'home-featured'

Wordpress使用_wp_attachment_metadata作為meta_key將生成的圖像的序列化數據保留在postmeta表中。

就您而言,如果您的代碼不需要完整尺寸的圖像,請將_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',
                ),
        ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM