Skip to main content
Completely changed my initial answer to something way more complete and useful.
Source Link

Initially, I would guess there is an issue insaw 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:

echo '<div$post_thumbnail_id class="post_thumb"= style="background-image:url('.get_the_post_thumbnailget_post_thumbnail_id($post->ID);

So, I used @james-barrett's suggestion and changed it to:

$post_thumbnail_id $recent['ID'],= 'thumbnail').'get_post_thumbnail_id($recent["ID"])"></div>';;

The singleWhich 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 double quotes are conflictingdiscovered 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 have had issues likefound might not work with other sizes, or perhaps you could do more research after this, try usingbut I changed the $post_thumbnail_url variable to this:

$post_thumbnail_url = wp_get_attachment_thumb_url( $post_thumbnail_id );

Which resulted in the 150x150 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('\''.get_the_post_thumbnail($post_thumbnail_url.'\')"></div>';
 $recent["ID"], "thumbnail")      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
        }
?>

If that doesn't workNote: I adjusted the url() to be url('') because some of my older browsers require the single quotes, then webut I suspect you can try more complex answersremove them.

I would guess there is an issue in this line:

echo '<div class="post_thumb" style="background-image:url('.get_the_post_thumbnail( $recent['ID'], 'thumbnail').')"></div>';

The single and double quotes are conflicting. I have had issues like this, try using this:

echo '<div class="post_thumb" style="background-image:url('.get_the_post_thumbnail( $recent["ID"], "thumbnail").')"></div>';

If that doesn't work, then we can try more complex answers.

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 150x150 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.

Source Link

I would guess there is an issue in this line:

echo '<div class="post_thumb" style="background-image:url('.get_the_post_thumbnail( $recent['ID'], 'thumbnail').')"></div>';

The single and double quotes are conflicting. I have had issues like this, try using this:

echo '<div class="post_thumb" style="background-image:url('.get_the_post_thumbnail( $recent["ID"], "thumbnail").')"></div>';

If that doesn't work, then we can try more complex answers.