WordPress.org

Ready to get started?Download WordPress

Forums

Display the post only if thumbnail is set up (17 posts)

  1. rosacreative
    Member
    Posted 4 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; ?>
  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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 } ?>
  3. rosacreative
    Member
    Posted 4 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() )

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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 } ?>
  5. rosacreative
    Member
    Posted 4 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; ?>
  6. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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...

  7. rosacreative
    Member
    Posted 4 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.

  8. rosacreative
    Member
    Posted 4 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 } ?>
  9. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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; ?>
  10. rosacreative
    Member
    Posted 4 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?

  11. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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

  12. amsul
    Member
    Posted 3 years ago #

    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?

  13. Acts7
    Member
    Posted 3 years ago #

    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.

  14. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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); 	?>
  15. Acts7
    Member
    Posted 3 years ago #

    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?

  16. Acts7
    Member
    Posted 3 years ago #

    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!

  17. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic