WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Random Posts From Specific Category – with Custom Fields

[Resolved] Random Posts From Specific Category – with Custom Fields

  • I’m trying to get wordpress to display random posts from a specific category, while using their Custom Field images.

    This is the code I am currently using. How can I fix that so it displays a random post instead of the most recent?

    <?php query_posts("showposts=6&category_name=PICS"); $i = 1; ?>
          	<?php while (have_posts()) : the_post(); ?>
    	<div class="clearfloat">
    	<?php $values = get_post_custom_values("Image");
    	if (isset($values[0])) { ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> 
    
    <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=150&h=115&zc=1&q=100"
    alt="<?php the_title(); ?>" class="center" width="150px" height="115px"  /></a>
    <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
      <?php } ?>
    
          <?php endwhile; ?>
Viewing 1 replies (of 1 total)
  • Add the orderby=rand attribute to the query_posts function as follows:

    <?php query_posts("showposts=6&category_name=PICS&orderby=rand"); $i = 1; ?>
              <?php while (have_posts()) : the_post(); ?>
        <div class="clearfloat">
        <?php $values = get_post_custom_values("Image");
        if (isset($values[0])) { ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> 
    
    <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=150&h=115&zc=1&q=100"
    alt="<?php the_title(); ?>" class="center" width="150px" height="115px"  /></a>
    <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
      <?php } ?>
    
          <?php endwhile; ?>
Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Random Posts From Specific Category – with Custom Fields’ is closed to new replies.
Skip to toolbar