Last active
December 18, 2015 14:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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