WordPress CMS: testimonials, custom fields, and random posts

Today I had to set up a way to organize and publish the content of customer testimonials. Excerpts from these testimonials were going to appear in the left column of the page, in addition to being able to viewed in their entirety. So the designer of the site and I both agreed they should be Posts, submitted within a Testimonial category.

On the page where the testimonial should appear, the client would have the option of getting a specific Testimonial to appear in the left column. However, if the client forgot to specify a Testimonial post, then a random testimonial would appear instead.

Here is the code that went in index.php…

<?php
    if(in_array("testimonial_id", get_post_custom_keys())){
        $custom_fields = get_post_custom();
        //get_custom_field_value() is in functions.php
        $excerpt_id = get_custom_field_value('testimonial_id', $custom_fields);
        $wp_post_obj = get_post($excerpt_id);
    }
    else{ //no testimonial_id set, so use random testimonial post
        $rand_posts = get_posts('showposts=1&category_name=Testimonial&orderby=rand');
        $wp_post_obj = $rand_posts[0];
    }
    $testimonial_custom_fields = get_post_custom($wp_post_obj->ID);
    $blockquote = $wp_post_obj->post_excerpt;
    $cite = get_custom_field_value('testimonial_person', $testimonial_custom_fields);
    $location = get_custom_field_value('testimonial_location', $testimonial_custom_fields);
?>
<blockquote><?php echo($blockquote); ?></blockquote>
<p><cite><?php echo($cite); ?></cite></p>
<p><?php echo($location); ?></p>

There are a couple of notable things about that code. First, we test to see if the client put a testimonial_id custom field in the page where the testimonial appears. If the custom field is there, then we retrieve the value of it with a custom function stored in functions.php (more on that below). If not, then we get a random post within the Testimonial category. Second, we take the post object and retrieve the Excerpt from it, as well as two custom fields using our same custom function.

Here is the code that goes in functions.php…


/* ********************************
get_custom_field_value($custom_field_key, $custom_fields_array)

purpose:
    to retrieve the value of a custom field.
args:
    $custom_field_key : the name of the custom field.
    $custom_fields_array : the associative array of custom fields returned by get_custom_fields().
returns:
    the value of the custom field, or if the custom field does not exist, an empty string.
example:
    $custom_fields = get_post_custom();
    $excerpt_id = get_custom_field_value('testimonial_id', $custom_fields);
    - or further reduced to -
    $excerpt_id = get_custom_field_value('testimonial_id', get_post_custom());
******************************** */
function get_custom_field_value($custom_field_key, $custom_fields_array) {
    if(isset($custom_fields_array[$custom_field_key])){
        foreach ($custom_fields_array[$custom_field_key] as $key => $value) {
            $return = $value; //use the last one, if the custom field is accidentally used more than once.
        }
        return $return;
}
    else{
        return "";
    }
}
This entry was posted in PHP, Web Development, WordPress. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>