I'm trying to get my code to:

  • Get a post's thumbnail url
  • or (if no thumbnail there) to
  • Echo a default url

I'm not sure if I am going the easiest way or right way about this.

This is the code so far. $postid being the attribute for the id of the post I want the url from.

// Add Shortcode
function friend_pic( $atts , $content = null ) {

    // Attributes
    extract( shortcode_atts(
            'postid' => '',
        ), $atts,

    $postid = $atts['postid'];


// shortcode contents from here

 echo "<img src=\"";
     if ( $postid ) {
      $thumb_url = wp_get_attachment_url( get_post_thumbnail_id( $postid, 'thumbnail', false) );
      $thumb_url = $thumb_url[0];
      echo "http://www.website.com/default.jpeg";
    echo "\">"; 

// Shortcode edit ends here

$output = ob_get_contents();

    /// FINAL OUTPUT ////
    return $output;
add_shortcode( 'f-p', 'friend_pic' ); 

**Aditional Details* The point of this is to display the pictures of my friends by using a shortcode in my posts. The pages of my website are about my family and friends, and when I want to reference one I just wanna show the thumb of their page, its their picture, and if there isnt a thumb then show a default picture (a N/A picture).

  • can you post the full code of the shortcode function? btw, shortcode functions should not echo the result.
    – Michael
    Jul 10 '14 at 21:16
  • Hi Michael. I didn't add it all cuz the rest of the shortcode don't affect this. And the end of the code is not echoed. I can add it if you want. However all this current code does it trying to find the thumb of a wp post and echo the URL if there isn't to echo a default one.
    – user51030
    Jul 11 '14 at 1:09
  • Have to disagree with the reason this question was put on hold. "specific to wordpress" is clearly met if he's trying to use a wp function. I could be wrong.
    – jdm2112
    Jul 11 '14 at 2:00

I checked it out further and there were a couple of problems with your code.

  • your if statement checks for a post ID, but you don't have a check for whether or not there is actually a featured image set, so I added a condition to the if statement
  • your closing bracket for get_post_thumbnail_id() was misplaced
  • you were not actually echoing $thumb_url
  • wp_get_attachment_url() returns the image src (not an array) and does not take any additional parameters beyond the attachment ID, which meant that $thumb_url[0] was returning just the first letter of the image src. Looks like you meant to use wp_get_attachment_image_src() which takes an image size parameter. (See codex: Function Reference/wp get attachment url, Function Reference/wp get attachment image src)

Here my modified version of your code:

echo "<img src=\"";
if ( $postid && get_post_thumbnail_id( $postid)) {
  $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id( $postid), 'thumbnail', false );
  $thumb_url = $thumb_url[0];
  echo $thumb_url;
  echo "http://www.website.com/default.jpeg";
echo "\">"; 

I would add that in the future, a good rule of thumb when coding is to take things a step at a time, rather than writing a whole bunch of code at once. Do it one line at a time and make sure that your variables contain the expected output before moving on to the next step. Otherwise, you can end up with a chunk of code like this that actually has problems in several spots and it can be difficult and/or time consuming to try and figure out why it's not working.

  • Its working perfect. I thank you for your time and the explanations. You're right about my coding "behaviour", its a bad habit I have, definitely learning that doing it right works wonders. Thanks again Kisabelle
    – user51030
    Jul 11 '14 at 23:58

You need to escape the quotes in your echo statements:

echo "<img src="";

should be changed to:

echo "<img src=\"";

or you could use single quotes so that you don't have to escape the double-quotes:

echo '<img src="';

Similarly, your last line of code should be changed to:

echo "\">"; 
  • Thx Kisabelle. Already are there. I just didn't add them to my code on the site😊 sry!
    – user51030
    Jul 11 '14 at 1:05

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.