Show X posts randomly from particular Category
-
I’m currently trying to display 3 posts from a particular category sorted randomly in my sidebar using a query_post, and then display an image from a custom field value. Currently I have two categories, and the following php:
<?php $args=array( 'cat'=>3, 'orderby' => 'rand', 'showposts' => 3 ); query_posts($args); ?> <? if (have_posts()) : while (have_posts()) : the_post(); ?> <li><?php $featuredimage = get_post_custom_values("featured-image"); if ( $featuredimage != '' ) { ?> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo get_option('home'); ?>/wp-content/uploads/<?php echo $featuredimage[0]; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" class="featured-image" /></a> <?php } ?></li> <?php endwhile; else: ?> <?php _e('Sorry, nothing matched your criteria.'); ?> <?php endif; ?>
Sometimes it shows nothing at all, but other times shows the image fine.
My logic tells me that it’s going through all the categories randomly right?
Can someone tell me how to make this work and show 3 random posts from category 3… ALL THE TIME regardless of whether on a page, post, archive etc?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Show X posts randomly from particular Category’ is closed to new replies.