0

I have a WordPress blog which is hosted as a sub directory of a web application. The WordPress installation is untouched and modifications are done at 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 do not always have image-name-300x300.jpg for home-featured. Instead they have random values 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

This question is not about WordPress, but about PHP. Anyway, if you know the full image name, you know the desired filename has the following structure: imagename-300x*. This you can feed to the PHP function glob, which will return an array of all files matching the given pattern. So, supposing $image contains the path to the full image you would do:

$matches = glob ($image . '-300x');
$homef-image = $matches[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.