Initially, I saw the double and single quotes all jumbled up. This did fix some of what I saw, but I finally had time to test the code you put up.
First step, I used var_dump
on $recent_posts
to make sure I was getting the usable values.
Next, I echoed each variable as it was called, to make sure I knew what was being generated. I discovered that this line, as @james-barrett said, was not calling correct information:
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
So, I used @james-barrett’s suggestion and changed it to:
$post_thumbnail_id = get_post_thumbnail_id($recent["ID"]);
Which then worked to get the correct ID.
After I was calling the correct information, the url()
portion was filling correctly, but the next problem was getting the correct size called.
I did some research and discovered how the files were saved in the ‘uploads’ folder and then continued on to search for a way to access a function that would allow me to reach the one labled “thumbnail”. The solution I found might not work with other sizes, or perhaps you could do more research after this, but I changed the $post_thumbnail_url
variable to this:
$post_thumbnail_url = wp_get_attachment_thumb_url( $post_thumbnail_id );
Which resulted in the 150×150 image being called.
After all was said and done, I converted your broken code above to this working code:
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$post_thumbnail_id = get_post_thumbnail_id($recent["ID"]);
$post_thumbnail_url = wp_get_attachment_thumb_url( $post_thumbnail_id );
echo '<div class="post">';
echo '<div class="post_thumb" style="background-image:url(\''.$post_thumbnail_url.'\')"></div>';
echo '<div class="content"';
echo '<h2 class="post_title">';
echo '<a href="'.get_permalink($recent["ID"]).'" title="'.$recent["post_title"].'" >'.$recent["post_title"].'</a>';
echo '</h2>';
echo '<p class="post_excerpt">'.$recent["post_excerpt"].'</p>';
echo '</div>'; //content
echo '</div>'; //.post
}
?>
Note: I adjusted the url() to be url(”) because some of my older browsers require the single quotes, but I suspect you can remove them.