WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Random Text/HTML Using PHP (9 posts)

  1. Connor Crosby
    Member
    Posted 3 years ago #

    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!

  2. Connor Crosby
    Member
    Posted 3 years ago #

    /* bump */

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

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

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

  4. theApe
    Member
    Posted 3 years ago #

    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?

  5. Connor Crosby
    Member
    Posted 3 years ago #

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

  6. Connor Crosby
    Member
    Posted 3 years ago #

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

  7. Connor Crosby
    Member
    Posted 3 years ago #

    That seemed to do the trick. Thank you!

  8. theApe
    Member
    Posted 3 years ago #


  9. Connor Crosby
    Member
    Posted 3 years ago #

    Oops, I meant quote, not blockquote.

Topic Closed

This topic has been closed to new replies.

About this Topic