Community Forums › Forums › Archived Forums › General Discussion › Can't get the post thumbnail to display
- This topic has 16 replies, 3 voices, and was last updated 9 years, 9 months ago by
cosmocanuck.
-
AuthorPosts
-
July 13, 2015 at 12:10 pm #159291
cosmocanuck
MemberHi all! I'm building an email template from scratch in my theme folder, for which I actually have tossed almost all the Genesis stuff, but I suspect this problem is still Genesis-related...
I have a custom query in there which grabs my latest blog post title and excerpt. But I can't for the life of me figure out how to get the post thumbnail in there as well!
Here's my code for the "core" of the query:
if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>'; echo '<p>' . get_the_excerpt() . '</p>'; echo '<p>' . wp_get_attachment_image_src($image_id, $imagesize, true) .'</p>'; echo '<p style="font-weight: bold;"><a href="' . get_the_permalink() . '">Read article</a></p>'; }
But the wp_get_attachment_image_src line only produces the single word "array".
Other options I've tried for this line include:
echo '<p>' . wp_get_attachment_url( $id ) .'</p>';
get_the_post_thumbnail($post->ID, 'content_archive_thumbnail')
etc. But nothing shows up.
I hope someone can steer me in the right direction or advise as to why the above are all failing to make anything show up...
Thanks!
http://adamabramsdesign.com/mailout/
adamJuly 13, 2015 at 12:43 pm #159296Brad Dalton
ParticipantJuly 13, 2015 at 7:50 pm #159338coralseait
MemberSo your image is coming back as an obj or array, so you'll need to reference that properly.
Also what is setting your $image_id, $imagesize when you call wp_get_attachment_image_src ?
do something like:
$my_image = wp_get_attachment_image_src( $image_id, $imagesize, true);
var_dump( $my_image);so you can see what the resultant array contains.
July 13, 2015 at 7:52 pm #159339Brad Dalton
ParticipantJuly 13, 2015 at 7:53 pm #159340coralseait
MemberExactly, I'm thinking nothing is set and we didn't get a full code paste to verify so that's likely the problem.
July 14, 2015 at 9:50 am #159384cosmocanuck
MemberThanks everyone for the quick response! I guess I should have included the entire query from the get-go. It's below.
I guess I'm running into trouble by assuming that since things like get_the_permalink() and get_the_excerpt() are working "out of the box" in my query, that referencing the thumbnail would work the same way. But I don't seem to have a handle on proper use of the thumbnail-related code (and have had a hard time finding a good summary of this info so far, just many variations on how it's done).
$include = get_pages('include=717'); $content = apply_filters('the_content',$include[0]->post_content); echo '<em>' . $content . '</em>'; /* Get the latest message */ echo '<h4>LATEST ARTICLE</h2>'; $args = array( 'category__in' => 19, // exclude posts from this category 'posts_per_page' => 1, ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>'; echo '<p>' . get_the_excerpt() . '</p>'; echo '<p>' . wp_get_attachment_image_src($image_id, $imagesize, true) .'</p>'; echo '<p style="font-weight: bold;"><a href="' . get_the_permalink() . '">Read article</a></p>'; } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata();
July 14, 2015 at 10:05 am #159389cosmocanuck
MemberGuys, I'm sorry - there actually was no featured image set for the post! Kinda explains why nothing was being displayed.
I was fooled into thinking there was one because my blog page view (http://adamabramsdesign.com/category/blog/) includes an image which looks convincingly like a Featured Image. But it's acquiring this another way. How, I can't even determine just now (this was one of my first themes built by modifying the Genesis child theme). But that's a question for another day I guess!
Thanks for all your help.
July 14, 2015 at 10:19 am #159392cosmocanuck
MemberArgh! Still running into some stuff I don't understand. First, I'm getting the word "Array" before my thumbnail image. And, though the thumbnail appears at a perfect size when I simply have it on the page, when I place the thumbnail code into a table cell, it shrinks down to a tiny size. What gives?
Here again is the page: http://adamabramsdesign.com/mailout/
And here's my new code:
// The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<table><tr> <td colspan="2"> <h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3></td></tr>'; echo '<tr><td>' . $thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail'); echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . the_title_attribute('echo=0') . '" >'; echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); echo '</a></td>'; echo '<td><p>' . get_the_excerpt() . '</p>'; echo '<p style="font-weight: bold;"><a href="' . get_the_permalink() . '">Read article</a></p>'; echo '</td></tr></table>'; } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata();
Thanks in advance....!
July 14, 2015 at 5:44 pm #159419Brad Dalton
ParticipantJuly 14, 2015 at 7:56 pm #159431cosmocanuck
MemberHmm, that didn't seem to have any effect, braddalton. I will keep digging into this issue as it can't be that tricky to use a thumbnail in a query... I hope...!
July 14, 2015 at 8:05 pm #159432Brad Dalton
ParticipantJuly 14, 2015 at 8:35 pm #159435coralseait
MemberWhat's setting your $thumbnail? As later you do $thumbnail->ID but I don't see $thumbnail set anywhere.
Just as an FYI, and this is personal preference, I HATE not declaring variables and using the proper get / set api / functions to set them etc and relying on globals or implicit declaration and assignment etc. PHP drives me nuts with it's mutability and all that, so I'm anal and declare everything and make sure I'm setting things properly and not relying on globals etc that should or may be there.
Are you sure $thumbnail is being set properly? Same for $post_id, are you sure it is being set properly?
July 14, 2015 at 10:52 pm #159439cosmocanuck
Membercoralseait, your questions are great and highlight the fact that I can't answer them, since I'm a bit falling into the trap of using "code that someone says should work" without fully understanding it. Bad, bad idea, I know. Sometimes the coding success and the comprehension of same play a bit of leapfrog. 8^)
That said, I'm not at all sure that $thumbnail or $post_id are being set properly - and I don't actually know how to find that out! I do want to conform to best practices as I continue to develop my coding skills so any advice or helpful links in this regard would be most welcome.
July 14, 2015 at 11:25 pm #159443coralseait
MemberOk, try something like this;
Mainly we've setup the global $post variable (I don't like doing this, but I can't test on your data an re-write it our preferred way, so ymmv).
We save off the post_id to a variable, and then anytime you want to call something or query something that uses post_id use the variable instead. I think this should correct your immediate issue. I haven't tested this because obviously I don't know what your posts look like.
// this will make post data available via the global post var $post global $post; // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); // ok so here we assign some vars we may use later // if you need others add them here // the post_id, if you need to call other functions // using post_id use this $csit_post_id $csit_post_id = $post->ID; // your thumbnail html $csit_post_tumbnail_html = get_the_post_thumbnail( $csit_post_id, 'thumbnail', array('class' => 'alignleft')); echo '<table><tr> <td colspan="2"> <h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3></td></tr>'; echo '<tr><td>' . // if I read this code right and interpret what you are trying properly; you don't really need this //$thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail'); echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . the_title_attribute('echo=0') . '" >'; echo $csit_post_tumbnail_html; echo '</a></td>'; echo '<td><p>' . get_the_excerpt() . '</p>'; echo '<p style="font-weight: bold;"><a href="' . get_the_permalink() . '">Read article</a></p>'; echo '</td></tr></table>'; } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata();
July 14, 2015 at 11:36 pm #159446coralseait
MemberNote, I haven't corrected your thumbnail link in the above code, which will need to be done as well ... but the idea is the same. Use the proper post_id via that csit variable we set to get whatever you need via the functions in the codex. If you describe what your intend all this to do, I can probably write it or improve it all for you.
I'm referring to whatever this line is intended to do:
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . the_title_attribute('echo=0') . '" >';
July 15, 2015 at 12:22 am #159449coralseait
MemberHowdy, less distracted now; had a bit more time. I've written this more to teach you how to properly save values and call functions and use the results. Try to read the codex and understand why you are calling things vs just pasting code!
// this will make post data available via the global post var $post global $post; // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); // ok so here we assign some vars we may use later // if you need others add them here // the post_id, if you need to call other functions // using post_id use $csit_post_id as an argument $csit_post_id = $post->ID; // there's a lot of way's to skin this cat, i'm showing you a couple // that may seem round a bout, but it is so you can learn how to call functions // properly and get what you need from the return. Learning to fish here ... // What you need to do is read the codex for how to retrieve what you want from // the db and what arguments to properly call those functions. There's efficient and // inefficient methods of doing so - this isn't meant to be THE WAY, but to show you // how to call things, save the results and use them, including using arrays // the featured image id, if you need to call other functions // using the featured image id use $csit_post_featured_id as an argument $csit_post_featured_id = get_post_thumbnail_id( $csit_post_id); // featured image url // wp_get_attachment_image_src returns an array, url is in element 0 of this array $csit_post_featured_src = wp_get_attachment_image_src( $csit_post_featured_id, 'thumbnail'); $csit_post_featured_url = $csit_post_featured_src[0]; // another way to do this, but i'll leave that up to you which you need // $csit_post_featured_url = wp_get_attachment_url( $csit_post_featured_id); // your thumbnail html $csit_post_featured_html = get_the_post_thumbnail( $csit_post_id, 'thumbnail', array('class' => 'alignleft')); echo '<table><tr> <td colspan="2"> <h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3></td></tr>'; echo '<tr><td>' . echo '<a href="' . $csit_post_featured_url . '" title="' . the_title_attribute('echo=0') . '" >'; echo $csit_post_featured_html; echo '</a></td>'; echo '<td><p>' . get_the_excerpt() . '</p>'; echo '<p style="font-weight: bold;"><a href="' . get_the_permalink() . '">Read article</a></p>'; echo '</td></tr></table>'; } } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata();
July 15, 2015 at 10:05 am #159481cosmocanuck
Membercoralseait, thanks so much! I'm actually heading out of town today so this will all have to go on hold for a little while, but I'll delve into it once I return.
Agreed that just pasting code is something to avoid! Thanks again for your help and I'll pick this up again soon.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.