Skip to content

Instantly share code, notes, and snippets.

@franhaselden
Last active October 21, 2022 08:26
Show Gist options
  • Save franhaselden/00213819224965ed8744 to your computer and use it in GitHub Desktop.
Save franhaselden/00213819224965ed8744 to your computer and use it in GitHub Desktop.
Wordpress trim content to 100 characters
<?php
// Trims the_content. In place of excerpt, this allows us to output line breaks and styling
$post_content = get_the_content_with_formatting();
// Checks the string length, if under 500 chars...
if (strlen($post_content) < 500){
// Outputs the whole post content
print $post_content . '...';
}else{
// Output only 500 chars, but don't cut halfway through a word
$post_trimmed = substr($post_content, 0, strpos($post_content, ' ', 500));
print $post_trimmed . '...';
}
?>
<?php
// Trims the_content. In place of excerpt, this allows us to output line breaks and styling
$post_content = get_the_content();
// Checks the string length, if under 500 chars...
if (strlen($post_content) < 500){
// Outputs the whole post content
print $post_content . '...';
}else{
// Output only 500 chars, but don't cut halfway through a word
$post_trimmed = substr($post_content, 0, strpos($post_content, ' ', 500));
print $post_trimmed . '...';
}
?>
@jcpux
Copy link

jcpux commented Nov 26, 2019

Cool.. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment