WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress Loop - skip posts without a thumbnail (2 posts)

  1. ironman95
    Member
    Posted 11 months ago #

    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'

  2. Girmay
    Member
    Posted 8 months ago #

    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

Reply

You must log in to post.

About this Topic