Random WordPress Quotes Without A Plugin

Random Clown (by trp0)

Last month as part of my (Anti) Social spring cleaning, I added new testimonials and added a big block of them to the sidebar on certain parts of my site.

Ajith noted that they were a duplication of the testimonials on the portfolio page, so I used is_page conditonally so they didn’t show up on the portfolio page.

I was never really happy with the way that they looked, even though I kept playing with the style. I decided what I wanted to do was have random testimonial quotes display in the header the way that my last tweet on twitter shows up in the header on the blog part of my site.

Plugin Solution

At first I tried the Stray Random Quotes plugin. I could add each testimonial as a quote entry and they would be displayed wherever I placed the code on the page. I added it to the header where my last tweet is on my blog page. This worked fairly well. The plugin has a settings page to style rather than providing a class and that confused me for a little bit – I couldn’t get the styling just right, but I think I was making it unnecessarily difficult. I got it all set up and found that pages were loading slower. I decided to take a break to think about the best way to handle this. I also had Stratos in my head saying don’t use a plugin if you don’t have to.

Query Posts Solution

I then had a a-ha and duh! moment. Since the testimonials were already contained within posts for my portfolio, why not add the quote to the excerpt field and then pull them to the page using query posts and the excerpt similar to what I did with Thumbnails for Excerpts and Query Posts. The Query Posts could pull one excerpt from the Portfolio category and it could be ordered randomly so each time the page was loaded a different testimonial would be displayed.

I ran into a couple of problems with this solution.

  1. I can not use query posts in the header as there is already the main loop on the main part of the page. Query posts can not be used for multiple loops. The codex says that you can get strange results – and I did. The testimonial was showing up in the header but the post associated with the testimonial was being pulled to the page, even on pages like Resume where I don’t even have any posts. I had only used query posts in the sidebar before this so I hadn’t encountered this issue before. From the codex:

    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

    The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

    Too bad that I didn’t read that first. But, fortunately, I was enjoying learning and was not in fear for my sanity.

  2. I use the Thumbnails for Excerpts plugin on this site for my archives, search results, and category pages. Since I wanted to use an excerpt in the query post and my posts for the portfolio contain images, a thumbnail was being pulled into the header area along with the testimonial. Obviously, this didn’t look very good as there really isn’t room for an image, even a thumbnail, in that location.

So, my thinking was on the right track but I had to come up with a different solution.

Random Posts Solution

I then discovered that I could use the get_posts template tag instead of query posts. I could specify one post excerpt be pulled from one specific category and have the posts be displayed randomly. Here is the code that I used.

 $rand_posts = get_posts('numberposts=1&offset=1&
 foreach( $rand_posts as $post ) :
<div class="aktt_tweets">   <?php the_excerpt(); ?></div>
 <?php endforeach; ?>

This code is being used to display client testimonials on all pages except for the blog; Services, Portfolio, etc. Each time the page is loaded a different testimonial will be displayed. The link in the client’s site name was going to go to their site but I decided to be greedy and link it to my portfolio page ;-)


Of course, I still had the thumbnails issues discussed above. To get around that I did the following. This might not have been the cleanest solution but I created a new category called Testimonial and created a new post for each one. The post and the excerpt field only contains the quote from the client and nothing else. I am already using the Advanced Category Excluder Plugin so I simply excluded this new category from being displayed on the main page, archives or RSS feed. I also excluded it from the list of Categories in the sidebar.

I am also listing random quotes in the sidebar but I will talk about this later because this article is getting quite long. What do you think? Is this code solution better than using a plugin? I’m not sure – because of another plugin it became more plugin but I’m not willing to give up Thumbnails for Excerpts – it’s one of my favorite plugins.

photo credit: trp0

It's Time!

Start your development project now!

Contact Me! >>


Keep Reading:
How to Add Custom Taxonomies to Your WordPress Theme
How to Add a WordPress Default Post Thumbnail Image Without a Plugin
Spacing for Captions in the WordPress 3.5 Gallery
Upcoming Features in WordPress 3.5


  1. says

    Some really good deduction and detective work, and a great “un-plugin!” Congratulations!

    I don’t have a use for this right now, but one is brewing somewhere in the back of my old bald skull. I’ll bookmark it for when the idea raises its ugly head!

  2. says

    Well i am going to be a pain once more but i guess you are used to it. I think i have put the worm in you with the “make it as faster as you can”. But here is what i would do. I would have a plain text file with all the testimonials one per line. After that, i would have used the following code within a wordpress function:
    $file_contents = file_get_contents('quotefile.txt');
    $quotes = split("\n", $file_contents);
    echo $quotes[rand(0, count($quotes))];

    This way you avoid any database query and it’s pretty easy to maintain. Though i don’t know how you have organized things but if you needed just that this is what i would do.
    So, there. I was a pain once more :P

    stratosg´s last blog post – Member Of The Month: April

    • says

      Hi Stratos – I thought about the text file solution but I don’t really know how to use one.

      The code you wrote goes inside the function right? Then how is that called to the template? Or do I just name that function and make a call to that?

      I like the idea of not sending any queries. And then I could get rid of the “dummy” posts I made.

  3. says

    This is a clean and lightweight code, great!
    Is it possible to make a slide show of posts which can start on click or automatically? sorry for being off topic I was searching this function for a while now.

    Chinese Girl´s last blog post – Photo Shenzhen Marine World

  4. says

    So add this to my list of things I need done. I have no clue if people go to my testimonial page but now that you moved the blog over to my website and got it transferred to hostgator, I would love it if my testimonials from that page were visible on the blog page or the header with the sky. SO I leave it in your hands. Thank you for all you have done already. Hiring you was one of the smartest decisions I have made. Life is so much easier with an Expert!

    Julie Walraven´s last blog post – Blog has moved

    • says

      Hi Julie,

      Great! That sounds like a really good idea and will add it to the list. Stratos offered a cleaner solution so I will probably test that before doing the work on someone else’s site.

      And thank you :-)

    • says

      Hi Ajith – Thanks! And thanks for the inspiration to do the testimonials differently. Your idea about more testimonials … is perfect.

      Only the portfolio page has multiple looks – the others are static pages. It might be slightly slower but not much. The plugin really slowed things down.

      I’m going to try Stratos’ text file solution – that should be the fastest since I won’t be querying the database.

Comments are closed. Please contact me for specific questions or subscribe to my newsletter.