The other day someone asked a question in a WordPress related LinkedIn group looking for a reliable plugin that can show random testimonials.
For these type of things I really don’t want to rely on the whims of a plugin developer, I much rather roll my own function. As an additional bonus you can make it exactly as you like it without any unnecessary fluff!
To show random testimonials, we first need to put together the Testimonial custom post type. If you’re an avid reader of WP TIPS you already know that I use GenerateWP to generate my custom post types (and other general stuff). The site recently has undergone an update, go check it out!
This code goes into your
functions.php file or functionality plugin (minus the opening
<php). Nothing fancy is happening here and for the sake of simplicity I am also not going to add any taxonomies or custom meta boxes. The Testimonial custom post type will have a Title (which can be used for the name of the person leaving the testimonial), the editor to add the testimonial text, the featured image to show an image of the person and the excerpt which can be used to show random testimonials. After all, as most testimonials vary in length you probably only want to show a short line instead of the entire testimonial.
On line 42 you can see that I have added a nice Dashicon to show in the sidebar of the Dashboard. You will find the link to that tutorial in the Related Posts at the bottom.
Once you have filled in a couple of them it is time to write the function to show random testimonials on the front end of your website. To do so we can use the following function, which you can add to your
functions.php file or functionality plugin (again minus the opening
The above function actually still doesn’t output anything, it just shows the query to pick 4 testimonials at random. You can adjust that number to anything you like. As you can see, depending on what else you add /taxonomies, meta boxes, you can fill this in with anything you like, but for the sake of this tutorial I have decided on keeping things simple.
To show you what’s next I have also put together a quick example as a base:
The code above replaces the code of the earlier function from line 24-35 (
endwhile). As you can see each Testimonial is wrapped in a class of
random-testimonial. I then first show the thumbnail left-aligned and resized to 75×75 pixels. Then I show the excerpt that I have wrapped in a p class of
quote and then with right-aligned I show the title, which is the name of the person who wrote the testimonial.
Because I have wrapped the excerpt in its own class, you need to add a filter to remove the p tags WordPress automatically adds. I have added that filter all the way to the bottom; this line should go separately (i.e. it is not part of the loop) in your
Now it is time to finally show these random testimonials! Oh, before I forget, I changed the number of how many testimonials to show from 4 to 2.
You can basically add the function anywhere, for example on your homepage. I have quickly made a separate page template for it using a TwentyTwelve child theme. I have added all 3 files to the Gist, but because it automatically puts the files in alphabetical order, I will first address them and then show them.
I have used a Twenty Twelve child theme for the rest of this tutorial.
To make a Page Template I have duplicated the
full-width.php template, named it
testimonials.php and added
Template Name: Testimonials to the top. Now you can make a new Page in the WordPress back end and select this template for it.
As you will see the page template is tiny and has a call to the template part
content-page.php in it, so we’re going to add a condition there to show the testimonials.
Open the template part and add the lines to it. I also have wrapped the
so_rand_testimonials() function in an additional class.
Once you have saved this file, you only need to add some styling and you’re done.
You can see the result in the image next to the opening paragraph of this WP TIP.
So what do you think? Plugin or DIY?
Please let me know in the comments below.