Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Quotes
#1
Many people like to have some sort of general interest stuff in the sidebar such a thumbnail photo or a random quote. This will show how simple it is to generate a random quote in the sidebar. It is suitable for people with limited or no programming experience.

PHP has a handy function to return a random element from an array, so we will use that. We will create a component of quotes then put the component in the sidebar.

I like to keep things neat for editing, so you might like to notice that I use line breaks to suit myself and make it easy to edit, rather than in their perhaps 'natural' place.

First create the component; I call it "Random Quotes":
Code:
<small><u>Random Quotes</u></small><br />
<?php
$quotes=array(
'There is nothing more difficult to plan, more doubtful of success, more dangerous to manage than the creation of a new system.
<br /><span class="author">Machiavelli (1469 - 1527)</span>' ,
'...compared to an unbelievable variety of other animals, the hedgehog was quite fortunate
<br /><span class="author">Terry Pratchett, his famous running gag <i>Wyrd Sisters</i></span>'
);
echo $quotes[array_rand($quotes)];
?>
The first line is a small underlined heading (which would be better off defined in the theme's css, a task to do) , then some php code. We set up the array; each element is contained within two ' chars and separated with a comma; the last element does not need a comma after it. Close the array with a parentheses.

You should look out and check that the quote doesn't contain a ' character within it. Otherwise when PHP sees the ' character it thinks it has reached the end of the quote and will then likely get confused with the rest of the quote and probably just give up. If the quote does contain a ' character, either change it to a " character if you can, else edit the text to remove it (e.g. change "haven't" to "have not". You can't escape it with a backslash or two because the parser in the editor removes the backslashes. [EDIT removed reference to "put two \ characters in front of the ' character. The \\ is a special indication to PHP to ignore the following character (the ' ) and to keep going." The html parser will remove a backslash each time you use it. ]

The PHP function array_rand() returns an random index number into the array. We echo the element which has that index number. Save and we are done with the component.

You may notice how easy it is to add new quotes as we are using line breaks in the editor to suit ourselves. Just cut and paste, enclose within ' and a comma if needed.

By the way, the "author" class is defined in my custom theme's css file and simply makes the text small and coloured. It looks like this:
Code:
.author {
    font-size: 86%;
    color: #777;
  }
(I could improve the styling by removing the <br /> tag, changing spans to divs and putting a top-margin within the author class. I use the class elsewhere, so I haven't done that here.)

Now to include the random-quote component in the sidebar:
Code:
<p><?php get_component('random-quotes'); ?></p>

Save and we are done.

You can see the results here: http://www.nickcoleman.org
Reply
#2
I think the final comma is missing at the end of the Terry Pratchett quote in the component.

Thanks.

homershines
Reply
#3
I was wondering if I needed to be a bit more detailed so here is some more info for those new to PHP.

[EDIT] I moved some text and incorporated it into the first post. It no longer appears here.

There are plenty of sources of quotes on the web, you could also try searching for "fortune unix". Fortune is a famous quote program written for Unix machines and there are many publicly available lists of quotes for it. Personally I like to make up my own list as I go, adding memorable ones when I come across them.
Reply
#4
Thanks for the correction.

Do you think that adding the last bit of info into the first post would be helpful? I know for me, step by step instructions really help when I know little about the behind the scenes process. Then after the fact, I can review the steps and learn more about their purpose.

It is truly amazing the value of good instructions.

I'm working on a group dynamics game that I am using as a tool for community organizers, and I can see just how everything can and does get lost in translation.

Thanks.

homershines
Reply
#5
Yes, you are right, I'll do that. I often forget we can edit the original.
Reply




Users browsing this thread: 1 Guest(s)