I had asked a question earlier, on getting post-thumbnails from another WP-site. There, the awesome Mike coded a SQL query whose results are then passes on to a PHP array. The piece of code from that answer that I want to modify is this.
$post_urls = $wpdb->get_col($sql);
if (is_array($post_urls))
echo implode("\n",$post_urls);
Now the array holds values in the form of <img src="https://wordpress.stackexchange.com/link/to/image.jpg"/>
. Now how do I..
- Call the WP
image_resize
function on each image URL? - Apply my own CSS class to each image?
UPDATE:
Thanks to Jan for the tips on optimizing the SQL query. Now the $post_urls
array will hold only plain URLs to the images. Can someone show me how to loop through each URL in the array and add prefixes (like <img src="https://wordpress.stackexchange.com/questions/6873/
) and suffixes (like " class="awesome-image"/>
) to each of them?
3 Answers
Calling image_resize()
would be difficult, since it would search for the image in your main blog’s upload directory. If it is a fixed image size I would just add it to the configuration of your photoblog, so it is created there when you upload new images (you can then rebuild your old image thumbnails).
In Mike’s answer you see that he adds the <img src="https://wordpress.stackexchange.com/questions/6873/
and "/>
parts in the SQL query. That’s nice, but you don’t have to do that. If you leave that off you get just the URL of the image and you can add everything later in PHP. I wrote an alternative version that will provide you with more raw information to play with.
$attachment_data_list = $wpdb->get_results( $sql );
if ( $attachment_data_list ) {
foreach ( $attachment_data_list as $attachment_data ) {
echo '<img src="https://wordpress.stackexchange.com/mysubblog/wp-content/uploads/";
echo $attachment_data->upload_relative_path;
echo '"/>';
}
}