0

I'm trying to have five advertisement locations, each pulling posts from the same custom post type, but also loading randomly. The only problem I have with my code below is that it will sometimes duplicate the posts shown. Is there a way so that all 5 advertisements display a random advertisement post without duplicating?

Here's one of the banners:

<div class="advertisement">
<?php query_posts('post_type=advertisement_posts&offset=1&orderby=rand&showposts=1');?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $advert_url = get_post_meta($post->ID, 'rba_advert_url'); ?>
<?php $thumbnail_id = get_the_post_thumbnail($post->ID);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
if(!empty($thumbnail_id)) {
$image_path =  thumbGen($link[1],234,116,"crop=1&halign=center&valign=center&return=1");
$image_all = get_bloginfo('url').$image_path;
$my_image = array_values(getimagesize($image_all));
list($width, $height, $type, $attr) = $my_image;
}
?>
    <?php if(has_post_thumbnail()) : ?>
    <a href="<?php echo $advert_url[0]; ?>" target="_blank">
    <img src="<?php echo $image_path; ?>" alt="<?php the_title(); ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>"/>
    </a>
    <?php endif; ?>

<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_query(); ?>  
</div>

Any ideas?

1 Answer 1

1

Fetch all five posts in one query, store the result in a variable and take one each time you want to show an advertisement.

Now you cannot get duplicates, and more important: you save four queries.

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.