WordPress.org

Ready to get started?Download WordPress

Forums

Random post from a random category (3 posts)

  1. Pete
    Member
    Posted 4 years ago #

    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; ?>
  2. MichaelH
    Member
    Posted 4 years ago #

    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&

  3. Pete
    Member
    Posted 4 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic