Array Function in WordPress 2.5

During a recent site redesign, the client required the site dynamically generate a unique promotional banner (in the sidebar) on certain high profile writers “about the author” page.

wordpress 2.5Before WordPress version 2.5.x, we had to rely on conditional tags such as is_page() or is_category() and numerous lines of code to accomplish it. For most parts it worked well, especially when you had only one or two conditions to satisfy, but when the number of such exceptions increased, the code became bulky and was prone to err.

With WordPress version 2.5.x, you can simplify the task by applying the conditions over an array of pages (categories, tags…), here’s an example:

Our conditional tag was required to automatically generate a banner when the author page of specific authors was queried.

We started off by simplifying the task by labeling all author’s banners with a unique identifier; in this example, we will use the author ID (which is the most unique table).

We then relied on the wp_query tag to set a variable called curauth (Current Author), more on custom author information can be found in Codex.

  1. <?php
  2. global $wp_query;
  3. $curauth = $wp_query->get_queried_object();
  4. ?>

We then followed it with an conditional tag that calls for an image with author page of authors listed in the array (works only with WordPress 2.5 and higher).

  1. <ul>
  2. <?php if (is_author(array(9,12,24,30,44,51,63,68,71,84,102,111,134))): ?>
  3. <li class=”author banner”>
  4. <img src=”<?php bloginfo(’stylesheet_directory’); ?>/images/authors/<?php echo $curauth->ID; ?>.jpg” alt=”<?php echo $curauth->first_name; ?>”/>
  5. </li>
  6. <?php endif; // end the if, no images for other author ?>
  7. </ul>

In this example, the array defines author IDs in ascending order, the images to be displayed are within a folder called authors inside the images folder for the theme. The above code works independent of “The Loop” (which is the heart of a WordPress theme that generate the posts list), but is activated only when a specific page type, in this case the author page, is dynamically generated.

The actual working example was set a little different as requested by the site owner, but the concept is the same. If you click on any of the authors name under the “Exclusive” list of posts (with the green background), the author page will display a small 163px by 78px banner in the left sidebar. Compare that with author pages for authors from the “Featured Post” list (with off-white background).

There are endless potential for the new array feature in WordPress 2.5, this function only reinforces WordPress towards becoming a full blown CMS!

Leave a Comments | Trackback | RSS 2.0

  1. 1. Tdude | May 22, 2008 #

    Thanx for the heads-up and sharing! This is very useful.
    All the best,
    T

Have Your Say »

(Required- use your name, not keywords)

(Required- will not be published)

(Optional)

Use SimpleCode while pasting codes.