Last active
October 21, 2022 08:26
-
-
Save franhaselden/00213819224965ed8744 to your computer and use it in GitHub Desktop.
Wordpress trim content to 100 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 | |
// 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 . '...'; | |
} | |
?> |
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 | |
// 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 . '...'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool.. thanks!