0

I am using the following code (see bottom) to display a custom post called 'artists'. I am also using <?php if (has_post_thumbnail( $post->ID ) ) so I can set the background of a div using the post thumbnail (I don't think this is the problem).

I want to display all of the posts on one page so use 'numberposts' => -1,. I have noticed that the posts after the first twelve posts are using another posts featured image if they do not have one assigned.

If I assign a featured image to any post in question then it does show the correct image. I just don't understand why every post after the first twelve are using another posts featured image. Is there something wrong with the loop as I'm using 'numberposts' => -1,? There's no harm in this as I'll be assigning an image to each one but want to understand why this might be happening.

enter image description here

   <?php

$args = array( 
    'post_type' => 'artists',
    'numberposts' => -1,
    'orderby' => 'title',
    'order' => 'ASC' 
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php endif; ?>

    <li class="artist-list artist-mobile">
        <a href="<?php esc_url( the_permalink() ); ?>">

                <div class="artist-box mobile" style="background-image: url('<?php echo $image[0]; ?>')">
                <div class="mobile-link"></div>
                <div class="info-back">
                <div class="info-post"><p>VIEW ARTIST</p></div>
                <div class="cross">+</div>
                </div>
                            </div>
                <div class="artist-name"><?php the_title();?></div>
        </a></li>
<?php endforeach; 
wp_reset_postdata();?>
7
  • <?php endforeach; ?> is not missing in your code, is it?
    – luke
    Commented Dec 16, 2013 at 16:55
  • Can we see the full loop code in context? Also, which specific post's featured image is being used out of place? Is it a specific post ID? Commented Dec 16, 2013 at 17:02
  • numberposts? what's that? I think that should be: posts_per_page. Commented Dec 16, 2013 at 17:04
  • 1
    @MayeenulIslam get_posts() takes numberposts as a valid parameter, interchangeably with posts_per_page, and converts numberposts to posts_per_page before passing the args array to WP_Query(). Commented Dec 16, 2013 at 17:06
  • I've added the full loop and a screenshot as you can see they are using a few different post's featured image. Not a specific post ID. Commented Dec 16, 2013 at 17:15

1 Answer 1

0

If I assign a featured image to any post in question then it does show the correct image. I just don't understand why every post after the first twelve are using another posts featured image.

I think the problem is that you do not reset $image if the current post doesn't have a featured thumbnail.

You should add for example:

<?php 
if (has_post_thumbnail( $post->ID ) ): 
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
else:
    $image = array( 'http://example.com/default.jpg' );
endif; 
?>

since you are using $image[0] in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.