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 trialKonrad Pilch
2,435 PointsHOw 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
15,622 PointsHmm, 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
Joe Consterdine
13,965 PointsHave you get theme support for post-thumbnails in your functions.php file?
Konrad Pilch
2,435 PointsI did wrote that it works on my about and portfolio page.
Casey Ydenberg
15,622 PointsBecause 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
29,725 PointsAgreed 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
15,622 PointsYou're problem with get_field is the same actually. This function accepts the post id as an optional second argument.
Konrad Pilch
2,435 PointsHow can i change it so it directs me to custom fields?
Casey Ydenberg
15,622 Pointsget_field ('blog_title', get_option ('page_for_posts'))
Konrad Pilch
2,435 PointsThank you! And thats the properl way to do it?
Austin Whipple
29,725 PointsThis 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
2,435 PointsThat 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
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsThis 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
2,435 PointsKonrad Pilch
2,435 Pointsfunny..
the get field is getting it from roads, same as title.
Why is this blog page soo messed up xd