douru5373 2017-03-29 07:50
浏览 40
已采纳

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

I have WordPress blog which is hosted as a sub directory of a web application. The WordPress installation is untouched and modifications are done in the theme level. The web application home page needs to display the recent blog posts. So I have the following code for displaying the recent posts:

    $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;
    }

I am getting the full size image url. I have done resizing in my functions.php file in theme:

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

The problem is that resized images in uploads directory does not always have image-name-300x300.jpg for home-featured. Instead it have random value for height depending on the image's ratio in file name(Ex: image-name-300x220.jpg). And I have regenerated the thumbnails again but still same file name. How do I get 'home-featured' for above solution?

  • 写回答

1条回答 默认 最新

  • dongxu1029 2017-03-29 08:44
    关注

    Wordpress keeps serialized data of generated images in postmeta table with _wp_attachment_metadata as a meta_key.

    In your case, if you don't need full size images for your code, change _wp_attached_file to _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 will be similar to:

    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',
                    ),
            ),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染