0

I would like to call specific images in my template from the posts and pages of my choosing.

For example I want to these featured images on my home page;

  • About page
  • Project post
  • Another page

I have used the_post_thumbnail() on my archive pages and to call the image to it's particular post or page. But i can't find any information about calling specific featured images.

Does anybody know?

  • How are you determining the ID of the pages you list? What have you tried? Please post your current code. – Chip Bennett Jan 12 '14 at 0:47
1

I think you are looking for get_the_post_thumbnail() which takes accepts the post ID as a parameter. get_the_post_thumbnail() generates the entire image code. There is no need for you to try to create your own <img> tag.

It is used as follows:

echo get_the_post_thumbnail( $post_id, $size, $attr );

So for your example, remove the following:

<img src="<?php echo get_the_post_thumbnail( '25, medium' ); ?>">

and replace with:

echo get_the_post_thumbnail( 25, 'medium' );
  • Thanks helgatheviking. That is indeed the code I want to use. But knowing how to use it would be helpful. I have done my research. At the moment I've found the id of the post which I would like to display the featured image of and passed it through the get_the_post_thumbnail function. Like so <img src="<?php echo get_the_post_thumbnail( '25, medium' ); ?>"> I also found this stackoverflow.com/questions/20681294/… I understand that this is what I'm looking for. I have no idea how to pass a variable in my template file. – JohnLion Jan 12 '14 at 12:42
  • Did you look at the codex page I linked you to? get_the_post_thumbail() isn't the source URL. It will print the entire image. I was adjust my answer based on the ID and size you are trying to use. – helgatheviking Jan 12 '14 at 14:11
  • I would like to print the entire image and not the source url. As you can see from the code I'm using above It's the same as your answer. That is what i want to do but it's not pulling in the image, it is showing a small icon where the image should be. In the question I linked you to, Chintan is saying that in the new wordpress you have to pass the post id as a variable, is this correct? If so could you give me an example? And yes, thank you i have read through the codex several times and always do as much reading and trials before posting a question. Thank you for your assistance so far! – JohnLion Jan 12 '14 at 14:42
  • This is the html output taken from chrome inspector of the image. <img src="&lt;img width=" 800"="" height="412" class="attachment-post-thumbnail wp-post-image" alt="882"> – JohnLion Jan 12 '14 at 14:51
  • You aren't using my code as I posted it, you are still trying to insert it as the src for the image. See edit. – helgatheviking Jan 12 '14 at 14:53

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.