joshrodgers
Member
Posted 1 year ago #
I have created a theme that uses a featured image on every page.
In settings, I have setup my "Posts Page" to be "news"...how do I get the featured image from "news" to display?
I have tried using: http://pastebin.com/ir5YJm2c and the standard: http://pastebin.com/yZv9yc2W, but neither one works...any ideas?
Thanks,
Josh
do you mean, you want to display featured images from another post ?
you can post up to 10 lines of code directly in the forum - just make sure to mark them using the code button, or wrap the code in backticks - see http://codex.wordpress.org/Forum_Welcome#Posting_Code
try:
<?php if( is_home() && get_option('page_for_posts') ) :
echo get_the_post_thumbnail(get_option('page_for_posts'));
endif; ?>
or the same as a one-liner:
<?php if( is_home() && $id = get_option('page_for_posts') ) echo get_the_post_thumbnail( $id ); ?>
joshrodgers
Member
Posted 1 year ago #
alchymyth,
I'm sorry, neither one of those work :(
I left them as is, I changed page_for_posts to be news, News, or 113 and the code still didn't work :(
Any Ideas...Thanks,
Josh
joshrodgers
Member
Posted 1 year ago #
blacklizt,
Nope...I have a featured image assigned to a page called "news". I did this by going to: Settings > Reading > and choosing "news" in the Posts Page dropdown.
Thanks,
Josh
joshrodgers
Member
Posted 1 year ago #
Ok,
So, after looking into the issue, I wrote the following:
<?php
$page_for_posts = get_option( 'page_for_posts' );
echo $page_for_posts;
echo get_the_post_thumbnail($page_for_posts, 'large');
?>
The only problem is that my ID echo's just fine, but it still doesn't display the thumbnail :(
Thanks,
Josh
If possible, paste the whole code from the template you are using for the "news page" into pastebin.com, also the code from functions.php. That may help pinpoint the problem.
joshrodgers
Member
Posted 1 year ago #
Ok,
Keep in mind I didn't write this code, I'm just tweaking it for a friend of mine: index.php...http://pastebin.com/YbdFNZ3j functions.php: http://pastebin.com/NP55A2bD
Thanks,
Josh
where in index.php would you have the code for the post thumbnail?
is it sthis line 11:
<div id="photo"><?php echo get_the_post_thumbnail( $post_id); ?></div>
if you - just as a test - switch to the default settings ('your latest posts') under 'settings' -> 'reading', does the thumbnail show in the 'news' page?
joshrodgers
Member
Posted 1 year ago #
alchymyth,
I feel like such an idiot!! I was trouble-shooting this last night and I guess I removed the featured image for the news page...so, of course, the image wasn't showing up!
I added the featured image and the following code:
<?php if(is_home()) { ?>
<?php
$page_for_posts = get_option( 'page_for_posts' );
echo get_the_post_thumbnail($page_for_posts, 'large');
?>
<?php } ?>
Now, everything works as expected...THANKS so much for your help!
Josh
If I'm understanding you correctly, this will work:
<?php
$news_page_featured_image = get_the_post_thumbnail( get_option( 'page_for_posts' ) );
?>
Question:
In what template file have you placed this code?
joshrodgers
Member
Posted 1 year ago #
Chip,
I have this code in index.php and everything is working great!
Thanks,
Josh
Glad to hear it!
P.S. another good sanity check here would have been to use has_post_thumbnail(), e.g.:
<?php
$homepage_has_featured_image = has_post_thumbnail( get_option( 'page_for_posts' ) );
?>
Might have saved a few pulled hairs. :)
joshrodgers
Member
Posted 1 year ago #
Chip,
You're right...those are good suggestions that I will definitely put to good use in the future (I like the hair on my head...lol)!!
Thanks,
Josh
I figure it's good, for those of you who still have hair on your head. ;)