rosacreative
Member
Posted 2 years ago #
I think I have successfully been able to display the last 3 thumbnails of a set of subpages in chronological order.
Now I'd like to add a conditional statement that will only display the post if the page has a thumbnail set up. I'm a novice with PHP. I'd appreciate the help.
This is the current code.
<?php query_posts( array(
'post_parent' => 12,
'post_type' => 'page',
'posts_per_page' => 3,) );
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php endwhile; endif; ?>
you could try to use:
</php if( get_the_post_thumbnail() ) { ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php } ?>
rosacreative
Member
Posted 2 years ago #
that didnt seem to work. any other suggestions? Im just not sure of the proper PHP to use.
this is the if statement I need to incorporate:
if ( has_post_thumbnail() )
with the if statement you suggested, it could look like this:
</php if( has_post_thumbnail() ) { ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php } ?>
rosacreative
Member
Posted 2 years ago #
When i use that, I only get 1 thumbnail displaying because its not in the loop. How can I incorporate the "if statement" into the loop?
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php endwhile; endif; ?>
as you may have noticed, the above code was meant just to show how to wrap a code into a conditional statement.
i leave this for you to figure out:
look at the code from your last post;
then look at the code from my reply just above that;
then compare the two codes and see where the difference is.
and i don't mean 'the loop', i mean where the conditional code is...
rosacreative
Member
Posted 2 years ago #
I understand. But there are two if's...I tried wrapping the if has_post_thumbnail around the whole loop, but was unsuccessful. :(
I'm creating my first wordpress theme and am no PHP-coder.
Thanks for helping though.
rosacreative
Member
Posted 2 years ago #
Is this what you were suggesting? It didnt work for me :(
<?php if( has_post_thumbnail() ) { ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php endwhile; endif; ?>
<?php } ?>
there was only two ways of doing it - you picked the other one.
(definitly very creative - true to your name - lol)
you need to be in the loop to know if the post has thumbnail, then you do the conditional statement:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if( has_post_thumbnail() ) { ?>
<div class="work-item"><?php the_post_thumbnail(); the_title(); ?>
<a href="<?php the_permalink() ?>">View Details</a>
</div>
<?php } ?>
<?php endwhile; endif; ?>
rosacreative
Member
Posted 2 years ago #
lol..Well I think i had tried both variations, but the variation you just posted, gives me NO thumbnails at all. Which is why I had to get creative! haha
Any idea why its not returning any thumbnails?
no idea, really.
i hope you still have this in front of the other code:
<?php query_posts( array(
'post_parent' => 12,
'post_type' => 'page',
'posts_per_page' => 3,) );
?>
this is the usage of 'has_post_thumbnail'
http://codex.wordpress.org/Template_Tags/has_post_thumbnail
hey rosa..
were you actually able to get a solution to this? i've been trying to figure out the same problem!
anyone else have a way to display a post only if it has a thumbnail while NOT effecting the loop post count?
Anyone else find a solution.
I think you're having the same issue as me.
I was the 5 most recent ONLY that have thumbnails.
The issue with the above code is, if you query 12 posts, maybe only 6 have thumbnails/featured images.
Then you only display 6.
What I want and I believe the original poster is asking is:
How can you ensure that you are delivering 12 posts that definately have thumbnail?
ie
post 1 (no thumb - so start showposts count over)
post 2 (has thumb - now only need 11 more posts WITH thumbnail)
post 3 (yes thumb - count is now 2 posts)
post 4 (no thumb - count remains at 2 posts)
etc.
following the docu from 'has_post_thumbnail()' to this:
http://phpxref.ftwr.co.uk/wordpress/wp-includes/post-thumbnail-template.php.source.html#l25
shows that the thumbnails are stored in a custom field with the key '_thumbnail_id'
then use the custom field parameter of query_posts()
http://codex.wordpress.org/Function_Reference/query_posts#Custom_Field_Parameters
query to get only posts with thumbnail:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // pagination
$args = array(
'posts_per_page' => -1, // optional to overwrite the dashboard setting
'paged' => $paged,
'meta_key' => '_thumbnail_id'
);
query_posts($args); ?>
http://phpxref.ftwr.co.uk/wordpress/wp-includes/post-thumbnail-template.php.source.html#l25
AWESOME! Thank you for that link, I didn't know it existed!
Could I ask, how did you know to look for the meta_key?
I'm trying to teach myself better standards of coding and research.
Was it because of the following:
You knew I was attempting to retrieve the thumbnail, so you look up the method of retrieving the thumbnail?
But then how did you know that you could retrieve it as a meta_id in a "query_posts".
I guess what I'm asking is could you let me in on how you found a method to this madness?
By the way alchymyth THANK YOU!!!!! For the speedy response and for helping me figure that one out. I was surprised that wordpress codex didn't have an obvious answer to that one.
Now there is an answer!
THANK YOU!
how did you know to look for the meta_key?
i followed the code of 'has_post_thumbnail()'
http://phpxref.ftwr.co.uk/wordpress/wp-includes/post-thumbnail-template.php.source.html#l12
which lead me to 'get_post_thumbnail_id()'
http://phpxref.ftwr.co.uk/wordpress/wp-includes/post-thumbnail-template.php.source.html#l25
which contained the return get_post_meta( $post_id, '_thumbnail_id', true ); code.
once you are in these PHPXRef 0.7 files/weblinks, you can simply follow one link after the other.
this is my general method to get more information out of the core files.