Options to Truncate Strings

Technique #1: Simple

function myTruncate($string, $limit, $break=".", $pad="...") {
        if(strlen($string) <= $limit) return $string; 
        if(false !== ($breakpoint = strpos($string, $break, $limit))) { 
            if($breakpoint < strlen($string) - 1) { 
                $string = substr($string, 0, $breakpoint) . $pad; } 
            } return $string; 
}
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Get Suffix of Given Number/Date

function get_suffix($number) {

   $last_number = substr($number,-1); //fetch the last number

   if($last_number == "0" || $last_number == 0){ $last_number = 4; } // if last number is 0 than it assign value 4
      return date("S",mktime(0,0,0,1,$last_number,2009));
}

Returns suffix of any number:…

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Highlight All Links To Current Page

$(function(){
       $("a").each(function(){
               if ($(this).attr("href") == window.location.pathname){
                       $(this).addClass("selected");
               }
       });
});

This function will add the class “selected” to any links (even relative) that point to the current page.…

Avatar of Chris Coyier
Chris Coyier on

Disable Automatic Formatting

Add to functions.php file

remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('the_title', 'wptexturize');

the wptexturize function is responsible for lots of automatic alterations to text stored in WordPress like automatic elipses (…), em and en dashes, typographers quotes, etc.…

Avatar of Chris Coyier
Chris Coyier on

Run a Loop Outside of WordPress

Include Basic WordPress Functions

<?php
  // Include WordPress
  define('WP_USE_THEMES', false);
  require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
  query_posts('showposts=1');
?>

Run Loop

<?php while (have_posts()): the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_excerpt(); ?>
   <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>

This can be used on …

Avatar of Chris Coyier
Chris Coyier on

Get URL Variables

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
Usage

Example URL:
http://www.example.com/index.php?id=1&image=awesome.jpg

Calling getQueryVariable("id") – would return “1”.
Calling getQueryVariable("image") – would …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Basic Link Rollover as CSS Sprite

a {
  background: url(sprite.png) no-repeat;
  display: block;
  height: 30px;
  width: 250px;
}

a:hover {
  background-position: 0 -30px;
}

The set height and width ensure only a portion of the sprite.png graphic is shown. The rollover shifts the position of the …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Return Only One Variable from MySQL Query

Function

function mysql_get_var($query,$y=0){
       $res = mysql_query($query);
       $row = mysql_fetch_array($res);
       mysql_free_result($res);
       $rec = $row[$y];
       return $rec;
}

Usage

$name = mysql_get_var("SELECT name from people where email = '[email protected]'");

Will return the name field, so what gets returned will be “Roger” (if …

Avatar of Chris Coyier
Chris Coyier on

jQuery Tweetify Text

Function

$.fn.tweetify = function() {
	this.each(function() {
		$(this).html(
			$(this).html()
				.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
				.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
				.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
		);
	});
	return $(this);
}

Usage

$("p").tweetify();

Before

<p>@seanhood have you seen this http://icanhascheezburger.com/ #lol</p>

After

<p><a href="http://twitter.com/seanhood">@seanhood</a> have you seen this
<a href="http://icanhascheezburger.com/">http://icanhascheezburger.com/</a>
<a 
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Using Custom Fields

Dump out all custom fields as a list

<?php the_meta(); ?>

Display value of one specific custom field

<?php echo get_post_meta($post->ID, 'mood', true); ?>

“mood” would be ID value of custom field

Display multiple values of same custom field ID

Avatar of Chris Coyier
Chris Coyier on (Updated on )