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.
<?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();?>
<?php endforeach; ?>
is not missing in your code, is it?numberposts
? what's that? I think that should be:posts_per_page
.get_posts()
takesnumberposts
as a valid parameter, interchangeably withposts_per_page
, and convertsnumberposts
toposts_per_page
before passing the args array toWP_Query()
.