WP Custom Function: rel_date()
Lately I’ve been working on a ‘Proof-of-Concept” for a large scale Enterprise site conversion to WPMU. This isn’t a complete redesign project, but rather a conversion to keep the same layout and functionality the current site offers, as well as to offer several features unavailable with the clients existing setup. The main feature missing from their existing setup is a CMS (content management system). This is where WPMU comes in! While it’s not the end all of CMS’s, it provides the groundwork for migrating away from static pages at an affordable price (FREE), not to mention all the other perks (great documentation, huge community support, an extensible plugin framework, etc.)! All that being said, here’s a little custom function I wrote for the project that may be useful for others:
First of all, to keep things clean and simple I created a “custom_functions.php” file in the root of the site’s theme folder to hold this function and all other functions you may need outside of the core WP functions. To make this file work, I added the following line of code to the bottom of the”functions.php” file in the /wp-content/themes/theme_name/ directory.
include ‘custom_functions.php’;
Now you can add your custom functions until your heart is content!
The existing site’s main pages has sections to list the current news and events. Here’s a snapshot of the area in question:

Even though you can’t tell from this image, the Events date reads “Today” when the events date is on the current day. This isn’t a feature native to WPMU. There are plugins that do similar things, but I figured a custom function for this would be simple and fun! First off, here’s my custom loop for the “Calendar of Events” section (keep in mind, some of this is specific to this sites CSS):
<!– Events Ticker Loop by: Joe–>
<!– Get 5 posts from the “News” category if they exist –>
<?php query_posts(’category_name=Events&showposts=5′); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!– Display the Date as abreviated month and day –>
<h3><?php rel_date(); ?></h3>
<!– Display posts as Post “titles” as permalinks –>
<DIV class=”post” id=”post-<?php the_ID(); ?>”>
<p class=”title”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a> | <?php echo get_post_meta($post->ID, ‘event_time’, true).”<br />”.get_post_meta($post->ID, ‘event_place’, true); ?></p>
<?php wp_link_pages(array(’before’ => ‘<p><strong>Pages:</strong> ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
</DIV><!– post –>
<?php endwhile; endif; ?>
</ul><!– events_nav –>
</div> <!– events –>
</div><!– right container –>
<!– End Events Ticker Loop by: Joe–>
The highlighted line shows the custom function call rel_date(). Here’s the code for the function:
function rel_date () {
$actual_date = date(’F j Y’);
$full_post_date = get_the_time(’F j Y’);
$short_post_date = get_the_time(’M j’);
$today = ‘Today’;
if ($full_post_date === $actual_date) {
echo $today;
} else {
echo $short_post_date;
}
}
This function stores the current date (Month, Day, and Year for mat) in a variable called $actual_date using the native PHP function date(), the date of the current post in the $full_post_date variable, the short version of the current posts’ date in the $short_post_date variable, and the string “Today” in the $today variable. Next the function compares the string values stored in the $full_post_date and $actual_date variables to see if they are equal. If they are, the function returns “Today”, if not it returns the abbreviated date of the post (Aug 30). Here’s a screen shot of the function in action:

I’ll follow with posts explaining some of the other custom bits in the “Events Ticker” code at a later date. Hope you enjoy!
-Phenix
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
Comments
// Begin Comments & Trackbacks ?>for the purposes of this post, I’m referencing Database driven CMS’s. I personally don’t normally refer to Contribute as a CMS since your content is not centrally stored, and you’re not able to control when and where your data is displayed dynamically. I also don’t want to get into any application that requires client side installation/maintenance.
-Phenix
Very nice. I know that it’s your job to handle web content anyways so this doesn’t completely apply but I always thought that little stuff like this is the most useful skill from knowing how to program. Having a working knowledge of how to code, there are so many small things that we can do to make some everyday processes easier for ourselves.
Nice function and thanks for sharing




Strictly speaking we do have a CMS. The Adobe Contribute system is a CMS, it’s just not a dynamic system that divorces the layout from the content.
Andy