WordPress Function to Style New or Updated Posts

Recently I’ve been doing a lot of WordPress theme customization work, and a client wanted to add those nifty “new” or “updated” icons to his post. I wrote this simple function to handle it.

Simply add this to your theme’s functions.php file, or if your theme doesn’t have a functions.php file, create one, and add the following also available as a zip: Add Style to New/Updates posts.

function pp_get_post_status() {
global $post;
//begin config Vars
$old_days = 2; //set how many days a post is considered "new"
$up_days = 1; //set how many days a post is considered "updated"
$new_class = 'new'; //class you want applied for posts considered "new"
$up_class = 'updated'; //class you want applied for posts considered "updated"
$class_tag = ''; //set default class, if any
//end config


$cur_time = time(); //get current system time
$post_age = $cur_time - get_the_time('U', $post->ID); //determine posts' age
$up_age = $cur_time - get_the_modified_time('U', $post->ID); //determine last update age
if($post_age < ($old_days * 86400)) {$class_tag = ' ' . $new_class;} //if post is newer than n days, apply "new"
elseif($up_age < ($up_days * 86400)) {$class_tag = ' ' . $up_class;} //if post older than n days, but updated within n days, apply "updated
echo $class_tag;
}

You can call it from within the loop, here is an example where it’s adding a class to the div containing the post, you could use it in any CSS class declaration. You will notice that it automatically adds the space between any existing classes and the class it adds.

<div class="article<?php pp_get_post_status(); ?>">

You could then use the CSS selector:
.article .new {style here}
for new posts and:
.article .updated {style here}
for updated posts.

Reblog this post [with Zemanta]
  • Share/Bookmark
Posted in IT | Tagged , , , , , , , | 2 Comments

Huntsville Tornado, maybe…

So we had a tornado in Huntsville this afternoon, passed by our house, by about a mile or so. Did quite a bit of damage I understand, we were out in the county, ironically directly in the path of the tornado, fortunately it fizzled out before it got to us.

I was out in the street taking photos, while my sweetie fled to the local storm shelter, which brings me to my main point: I LOVE Google voice, both because it lets me route calls between my contact number, and because it’s VERY entertaining. Here is the message (according to google voice transcriptions) that Shelli left on my phone.

Okay, and we are in a storm shelter. Andy Myer with me. My i think and I just laugh neat. I’m calling in thing and myneer. Anyway, it’s me. Capital last day to turn on the death wheel drive 9 in the back of the house vote like the last and it’s got a little refill, bureck so that’s what we are he. Anna, you can. Downtown take a few weeks of those of us while yet, hi bye.

I can’t believe I missed the last day to turn on the death wheel drive!!

Here are some of the pics I took:

Tornado dissipating

  • Share/Bookmark
Posted in Fun Stuff | 1 Comment

First image thumbnail, using PhotoBucket

I doubt there are a whole lot of people out there using WordPress like this, but, just in case, I thought I would put this up.
I created this script to help out @e_anurag, he had a unique situation, a blog with all of the images hosted over at photobucket.com. He wanted to use a thumbnail of the first image in each post on the homepage, but since the images weren’t hosted on the blog, the existing scripts he found wouldn’t work. I modified the display-first-image-as-thumbnail-script found at wprecipes.com

Photobucket automagically generates thumbnails of images uploaded, stored with the prefix ‘th_’ appended to the file name, thus ‘my_image.jpg’ becomes ‘th_my_image.jpg’ I modified the script so that it found two separate elements, the url path, and the image file name, allowing us to concatenate them together, with the ‘th_’ prefix stuck between. this script should go in you ‘functions.php‘ file in the root of your theme.

<?php
function photobucket_thumbnail() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('%<img.+src=['"]([^'"]+/+)(.*..*?)['"].*>%i', $post->post_content, $matches);
$first_img = $matches [1] [0];
$first_img .= 'th_';
$first_img .= $matches [2] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
?>

Using <?php photobucket_thumbnail() ?> anywhere within the WP loop, will return the URL for the thumbnail of the first image in the post, so long as it’s from photobucket.

Reblog this post [with Zemanta]
  • Share/Bookmark
Posted in IT | Tagged , , , , , | 1 Comment

Tweeps KML/Stream

So if you’ve been over to the “Stuff” page, you already know that I’ve started keeping up with the Tweeps that I’ve helped using a Google Earth map. I recently added a “tweetstream” of the people on the map. I thought of using a list to acquire the tweets, but I didn’t want to enter the usernames in 2 places, so I decided to instead parse the names out of the KML file. I used the native PHP extension simplexml to parse the KML file, and then used SimplePie to create a new RSS feed from all the users tweetstreams.

Updated: I have modified the script heavily, and added more comments in the code. Now I only retrieve one tweet from each user, then sort those tweets by date. The WordPress function I wrote now retrieves tweets until a certain number of characters is obtained, attempting to fill an area with tweets, regardless of individual tweet length.

Here is the code that creates the RSS feed:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
<channel>
<title>Twitter Helped KML</title>
<link>http://blog.perfectedperspectives.com/stuff</link>
<description>
Stream of tweets from tweeps I've helped with web development
</description>
<atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline/14388935.rss" rel="self"/>
<language>en-us</language>
<?php
//setup sortbydate
function sortbysub($a, $b) {
if($a['date'] > $b['date'])
return 1;
if($a['date'] < $b['date'])
return -1;
return 0;
}
//load existing KML file
$xml = simplexml_load_file('http://blog.perfectedperspectives.com/wp-content/uploads/TwitterHelped.kml');
//
foreach ($xml->Document->Folder as $folder) {
//parse each placemark for the TwitterUsername
foreach ($folder->Placemark as $placemark) {
$trimtweepurl = 'http://twitter.com/statuses/user_timeline/' . (string)trim($placemark->name, " @") . '.rss';
$tweepurls[] = $trimtweepurl;
}
}
//include SimplePie
include 'simplepie.inc';
//Setup error Checking
$firstfeed = 0;
//call SimplePie for each Tweeps RSS feed
foreach ($tweepurls as $simpleurl) {
//SimplePie parameters and setup
$feed = new SimplePie();
$feed->set_feed_url($simpleurl);
$feed->set_cache_duration (1000);
$feed->init();
$feed->handle_content_type();
//counter so that we only get the latest tweet from each tweep
$itemlimit = 0;
//retrieve the latest tweet from each feed
foreach ($feed->get_items() as $item) {
//count n tweet(s) from each user
if($itemlimit==1){break;}
//load tweet into array
$tweet['title'] = $item->get_title();
$tweet['link'] = $item->get_permalink();
$tweet['date'] = $item->get_date();
$tweet['guid'] = $item->get_id();
$tweet['description'] = $item->get_description();
//load each tweet as array element
$tweeptweets[] = $tweet;
//increment counter so we only get n tweets
$itemlimit = $itemlimit + 1;
//end foreach
}
//this if statement prevents the next from running on ALL RSS queries, this prevents having to wait for the entire script to run if the twitter ratelimit is exceeded from just the first RSS query
if($firstfeed > 0){break;}
if(in_array('<error>', $tweeptweets)){
//this is the error message displayed if Twitter denies our RSS query
?>
<item>
<title>Rate Limit Exceeded</title>
<link></link>
<pubDate></pubDate>
<guid></guid>
<description>Twitter HATES you!</description>
</item>
<?php
}
}
//here we sort the tweets by date, using the function declared earlier
usort($tweeptweets, 'sortbysub');
//flip into descending order
$tweeptweets = array_reverse($tweeptweets);
//create each item for the final rss feed
foreach ($tweeptweets as $tweeptweet) {
?>
<item>
<title><?php echo $tweeptweet['title']; ?></title>
<link><?php echo $tweeptweet['link']; ?></link>
<pubDate><?php echo $tweeptweet['date']; ?></pubDate>
<guid><?php echo $tweeptweet['guid']; ?></guid>
<description><?php echo $tweeptweet['description']; ?></description>
</item>
<?php }
//close the rss file
?>
</channel>
</rss>

I then created a shortcode in my functions.php file in my WordPress theme, using this code:

function display_helped_tweets() {
include_once(ABSPATH.WPINC.'/simplepie.inc');
$tweepstream = '<div id="tweepstream"><ul id="tweepstreamlist">';
$feed_url = 'http://tools.qtekso.com/pie/kmltwits.php';
$feed = new simplepie($feed_url);
foreach($feed->get_items() as $item) :
$tweetparts = explode(':', $item->get_title(), 2);
$tweepstream .= '<li class="tweepstreamtweet">';
$tweepstream .= '<a href="http://twitter.com/' . $tweetparts[0] . '" title="' . $item->get_title() . '">';
$tweepstream .= '@' . $tweetparts[0];
$tweepstream .= '</a>"' . $tweetparts[1] . '"</li>';
endforeach;
$tweepstream .= '</ul></div>';
return $tweepstream;
}
add_shortcode('tweepstream', 'display_helped_tweets')
Reblog this post [with Zemanta]
  • Share/Bookmark
Posted in IT | Tagged , , , , , , , , | 1 Comment