Skip to content

Instantly share code, notes, and snippets.

Last active December 18, 2015 14:29
What would you like to do?
Function to display a Wordpress string (e.g. post title or content) with a max number of characters.
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