WordPress.org

Support

Support » How-To and Troubleshooting » Random post from a random category

Random post from a random category

  • i love this nifty piece of code… it displays a random post from a random category 🙂 I’d love to see someone improve on it… I didn’t write this by the way.

    <?php $temp_query = $wp_query; ?>
    <?php $postMonth = date('m'); ?>
    <?php $postYear = date('Y'); ?>
    <?php query_posts('cat=315,317,280&showposts=5&orderby=rand&monthnum='.$postMonth.'&year='.$postYear); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> in
    <?php foreach((get_the_category()) as $childcat) {echo $childcat->cat_name;}?>	</li>
    </div>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • A random post from a random category could also be “a random post” so just delete this from the query_posts statement cat=315,317,280&

    Yep cool… I was wondering if I pit a minus sign before the cats then it would exclude them rather than include them as is now?

    <?php $temp_query = $wp_query; ?>
    <?php $postMonth = date('m'); ?>
    <?php $postYear = date('Y'); ?>
    <?php query_posts('cat=-315,-317,-280&showposts=5&orderby=rand&monthnum='.$postMonth.'&year='.$postYear); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> in
    <?php foreach((get_the_category()) as $childcat) {echo $childcat->cat_name;}?>	</li>
    </div>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Random post from a random category’ is closed to new replies.
Skip to toolbar