WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] WordPress Loop – skip posts without a thumbnail

[Resolved] WordPress Loop – skip posts without a thumbnail

  • I want skip every post that has no thumbnail. The code does not work properly yet.

    Actually the script doesn’t show posts without a thumbnail – that’s good, but in the loop the post with no thumbnail is still counted as a post.

    So when i have for example 10 posts in my wordpress database. I want show 5 of them. But only the posts who has a thumbnail.

    <ul>
    
        <?php
    
        $args = array(  'numberposts'  => 5,
                        'orderby'      => 'date',
                        'order'        => 'DESC',
                        'post_type'    => 'post',
                        'post_status'  => 'publish'
                    );
    
        $my_posts = get_posts( $args );
        global $post;
        foreach( $my_posts as $post ) : setup_postdata($post); 
    
        if ( !has_post_thumbnail() ) {
            continue;
        } else {
    
        ?>
    
    	    <li>
                <div class="clearfix" >
                    <div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
                    <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                    <p class="category"><?php the_category(', '); ?></p>
                </div>
    	    </li>
    
        <?php } ?>
    
        <?php endforeach; ?>
    
    </ul>

    Update:
    Simple add this
    ‘meta_key’ => ‘_thumbnail_id’

Viewing 1 replies (of 1 total)
  • You’re almost there…
    you will need to rewind the loop count right before you continue to the next post… get your current position first…$wp_query->current_post

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] WordPress Loop – skip posts without a thumbnail’ is closed to new replies.
Skip to toolbar