Forums

Show random page from selected set of pages (5 posts)

  1. tpflanz
    Member
    Posted 1 year ago #

    It may be easiest to show you what I need.

    http://oneroyalway.com/baseball-blog-directory/american-league-central/detroit-tigers-blogs

    If your reload this page a few times, you will see that it loads a random page.

    I currently have this set up with php code that replaces the loop, and instead loads a html page randomly.

    <?php
    $mypages = array("tigers1.htm", "tigers2.htm", "tigers3.htm");
    
    $myrandompage = $mypages[mt_rand(0, count($mypages) -1)];
    
    include ($myrandompage);
    ?>

    I want to be able to load a random WordPress post based on a set of post numbers.

    Thanks in advance for your help!

  2. tpflanz
    Member
    Posted 1 year ago #

    Why is it that only "softball" type questions ever get an answer here in the WordPress forums?

  3. jrav001
    Member
    Posted 1 year ago #

    Softball answer: Fill your array with post ID#'s and use get_post($myrandompage) to display that ID.

  4. tpflanz
    Member
    Posted 1 year ago #

    Does that work with pages as well? Could you elaborate as to the exact code that should be used

  5. jrav001
    Member
    Posted 1 year ago #

    You posted code above so I thought you were a coder and wanted to work out a different way of doing this....plus you made that condesending softball comment. ;-)

    If I wanted to display a random post from a batch of specific posts, I would create a new category, and select the posts I wanted randomized. Then do this:

    <?php query_posts('posts_per_page=1&cat=3&orderby=rand'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <h4><a href="<?php the_permalink(); ?>" title="Read the article &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h4>
    <?php the_excerpt(); ?>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>

    Note the top line. The "cat=3" refers to the category that contains the posts you want randomized. You can also do this with pages. See http://codex.wordpress.org/Function_Reference/query_posts for more info.

Topic Closed

This topic has been closed to new replies.

About this Topic