Skip to content

Instantly share code, notes, and snippets.

@gerbenvandijk
Last active December 18, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerbenvandijk/5797593 to your computer and use it in GitHub Desktop.
Save gerbenvandijk/5797593 to your computer and use it in GitHub Desktop.
Function to display a Wordpress string (e.g. post title or content) with a max number of characters.
<?php
function short_content($content,$limit) {
$content = strip_tags(html_entity_decode($content, ENT_QUOTES, "UTF-8"));
// when the content is longer then $limit, show three dots at the end
if(strlen($content) >= ($limit+3)) {
$content = substr($content, 0, $limit) . '...';
}
echo $content;
}
// Usage:
short_content(get_the_title($post->ID), 50); // Short title
short_content(get_post_field('post_content', $post->ID), 50); // Short content
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment