WordPress Guy

WordPress Guy designs and develops WordPress themes and plugins. WordPress Guy teaches you WordPress stuff.

Alert! This tip is more fun than useful.

Ever wanted to display a random “Read More…” link on your blog homepage? Here’s some code that’ll help you do just that.

The strings

Copy and paste this code somewhere in the index.php or functions.php file of your theme (but make sure you paste it before the loop):

<?php $more_strings = array("Read More...", "Keep on Reading...", "Wait! There's more...", "Read the rest of the article..."); ?>

You can add as many strings as you want.

Then change the the_content(); bit to this:

the_content($more_strings[rand(0,count($more_strings))]);

That’s it… Have fun!

Comments

  • May 21st, 2008 at 7:08 pm

    Wessley,

    Most of people aware of permalink function, but not much use it as Read More text. Those who would like to use Read more text out of the entry can use following snippet

    <a href="<?php the_permalink() ?>" rel="bookmark">Read More &raquo;</a>

  • May 21st, 2008 at 7:21 pm

    @Mehmet: That’s the beauty of WordPress… you can do things so many different ways. Thanks for the tip ;-)

  • May 23rd, 2008 at 5:55 am

    I love the random read-more text! It’s actually a great way to make each of your posts seem more individual and personal.

    Clever idea.

  • September 25th, 2008 at 6:04 pm

    This is brilliant. One slight problem, though:

    When using rand, you pass count($more_strings) as the upper limit. So if there are 4 strings, as in your example, you wind up with rand(0,4). PHP interprets that as actually 5 choices, so occasionally you wind up with an empty string “chosen.”

    So it should actually be:

    the_content($more_strings[rand(0,(count($more_strings) - 1))]);

    But really creative tip - it’s so good to be reminded of the power of PHP we have at our fingertips when using WP.

  • ... deux bouts de code dégottés sur wpguy affichent un lien “lire la suite” aléatoire pour chacun de vos articles. Le premier ...

  • January 23rd, 2011 at 5:34 am

    great information it is useful for me thanks for post stuff again

  • ... deux bouts de code dégottés sur wpguy affichent un lien « lire la suite » aléatoire pour chacun de vos articles. Le premier est à ...

  • July 19th, 2012 at 1:20 pm

    It is getting rain in Nepal however projects is still running on.

Your Comment