Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Konrad Pilch
Konrad Pilch
2,435 Points

HOw to set featurette image in wp

I made about and portfolio page, but on blog this code doesn’t work. It displays me an image from my post, why?

<?php
$thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
?>
<div class="intro-page" style="background:url('<?php echo $thumbnail_url; ?>'); background-size:cover; ">
   <div class="container"> 
      <div class="row">
         <div class="col-lg-12">
            <div class="intro-page-message">
               <h1><?php echo get_field('blog_title'); ?></h1>
            </div><!-- /intro-message -->
         </div><!-- /col -->
      </div><!-- /row -->
   </div><!-- /container -->
</div><!-- /intro-header -->

I pasted exactly same code in my about and portfolio so it works, but not on blog page, whats wrong with it?

4 Answers

Casey Ydenberg
Casey Ydenberg
15,622 Points

Hmm, criticism accepted.

I found this with some Googling: https://wordpress.org/support/topic/posts-page-id

Try using get_option('page_for_posts') in place of $post->ID

Konrad Pilch
Konrad Pilch
2,435 Points

This helped me thank you

$thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( get_option('page_for_posts') ) );

Now, the only thing it's not displaying me is this:

<h1><?php echo get_field('blog_title'); ?></h1>

Yet, works just fine on other pages.

WHy?

it didn't show even before. It never showed , same as the image.

Konrad Pilch
Konrad Pilch
2,435 Points

funny..

the get field is getting it from roads, same as title.

Why is this blog page soo messed up xd

Joe Consterdine
Joe Consterdine
13,965 Points

Have you get theme support for post-thumbnails in your functions.php file?

Konrad Pilch
Konrad Pilch
2,435 Points

I did wrote that it works on my about and portfolio page.

Casey Ydenberg
Casey Ydenberg
15,622 Points

Because Wordpress is stupid.

OK, serious answer: on your blog page, the default query is set up with your blog posts, rather than the actual page (yes, really). Therefore, $post->ID is the ID for the first post rather than the ID for the blog page.

The simplest work-around is to go to your admin panel, open the Blog page and look up the ID for that page, which is a variable in the URL when you're on the admin page. Then, hard code that ID into your code above instead of $post->ID.

Austin Whipple
Austin Whipple
29,725 Points

Agreed that some of the post thumbnail stuff can be a bit silly at times, requiring too much code to find just the URL of the image. However, you should always avoid hard coding post or page IDs into a theme if possible.

It may be simpler, but it can have terrible consequences for site maintenance and code reusability.

Casey Ydenberg
Casey Ydenberg
15,622 Points

You're problem with get_field is the same actually. This function accepts the post id as an optional second argument.

Konrad Pilch
Konrad Pilch
2,435 Points

How can i change it so it directs me to custom fields?

Casey Ydenberg
Casey Ydenberg
15,622 Points

get_field ('blog_title', get_option ('page_for_posts'))

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you! And thats the properl way to do it?

Austin Whipple
Austin Whipple
29,725 Points

This depends a bit where you're pasting the code. If it's inside The Loop of the proper page, it should probably work. Perhaps this support article with a couple alternatives will help?

Konrad Pilch
Konrad Pilch
2,435 Points

That doesn't work on me.

But thank you.

this

$thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( get_option('page_for_posts') ) );

worked.

Now it doesn't display my get_field as it didnt show me evnen before