• Resolved Connor Crosby

    (@ccmovies)


    I am making a theme and part of it shows a random quote. How can I use PHP to choose randomly a quote. Then, if they refresh the page, it shows a different one. I am not very good with PHP but I think it involves random or if/else. Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Connor Crosby

    (@ccmovies)

    /* bump */

    I am not very good with PHP but I think it involves random

    have you ever thought of searching the web for ‘random php’ which would have pointed you to a few possible sites with php tutorials?

    anyway, one possibility out of many:

    put your quotes into an array, and select a random counter; something along those lines:

    <?php
    $quotes = array(
    'Too good to be true',
    'The greatest prayer is patience  - The Buddha',
    'Another Quote',
    'If the WHY is big enough, the HOW looks after itself.'
    );
    $rand = rand( 0, count($quotes)-1 );
    echo $quotes[$rand];
    ?>

    https://php.net/manual/en/function.rand.php

    Where are you pulling the quotes from? Is it a set of posts, a separate file or are you listing them in the code and you only want a random one to appear?

    Thread Starter Connor Crosby

    (@ccmovies)

    I tried searching for it but I didn’t find anything. This will work if I have the text in blockquote right? Thank you!

    Thread Starter Connor Crosby

    (@ccmovies)

    I am listing them. I will not be using any WordPress code for that part. I would like one to be randomly chosen.

    Thread Starter Connor Crosby

    (@ccmovies)

    That seemed to do the trick. Thank you!

    Thread Starter Connor Crosby

    (@ccmovies)

    Oops, I meant quote, not blockquote.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Random Text/HTML Using PHP’ is closed to new replies.