If you were using a Wordpress as your CMS in your blog featuring some kind of random post of testimonials can bring useful purposes.
First, you can widen the possibility that your reader can see other post without navigating the older post button or links or simply make your old archive post visible randomly. Normally readers only read a couple of new post and leave the site if they got any valuable post. “Readers Behavior”.
One of my projects are asking me to have random testimonial excerpt in their site header so their client can view their other client testimonials which serve as encouragement. I was looking some plugins in the internet and found a lot of them. Since the client is only asking for a simple random functionality. I look for a better way (a code snippet) rather than to download a plug in and upload to their site which can add up to the slow load up.
This line code will filter the query based on the category name, the number of post per page, and take note: orderyby=rand is the one who make a random query. :
Code Snippet to Random Post:
<?php query_posts('category_name=YourCategoryName&orderby=rand&posts_per_page=1'); ?>
This line code will filter the query based on the category name, the number of post per page, and take note: orderyby=rand is the one who make a random query. :
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
You must change the YourCategoryName to whatever category you want to get the random posts. Change the number in the posts_per_page variable to the number of post you want.
After that you can now print the excerpt and the author information.
<?php $excerpt = get_the_excerpt(); echo $excerpt; ?> <div class="random-author"> <?php echo the_author_meta('last_name'). "-"; ?> <?php echo the_author_meta('first_name'); ?> <?php echo (','); ?> <?php echo (' '); ?> <?php echo the_author_meta('user_description'); ?> </div>Then you can close your looping with this code...
<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.', '') ?></p> <?php endif; ?>Complete Code for Random Post:
<?php query_posts('category_name=Testimonials&orderby=rand&posts_per_page=1'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <p> <?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15)."..."; ?> <div class="random-author"> <?php echo the_author_meta('last_name'). "-"; ?> <?php echo the_author_meta('first_name'); ?> <?php echo (','); ?> <?php echo (' '); ?> <?php echo the_author_meta('user_description'); ?> </div> </p> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.', '') ?></p> <?php endif; ?>
0 comments:
Post a Comment