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.
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.
- 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.
- 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.
<?php $rand_posts = get_posts('numberposts=1&offset=1& category_name=testimonial&orderby=rand'); 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
Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.