Skip to content

Instantly share code, notes, and snippets.

@khleomix
Created February 25, 2022 17:17
Show Gist options
  • Save khleomix/a5836501af6c326df4e7642755c2321b to your computer and use it in GitHub Desktop.
Save khleomix/a5836501af6c326df4e7642755c2321b to your computer and use it in GitHub Desktop.
Limit the content length and retain markup
<?php
/**
* Limit the content length.
*
* @author JC Palmes
*
* @param array $args Parameters include length and more.
*
* @return string The content.
*/
function return_the_content( $args = [] ) {
// Set defaults.
$defaults = [
'content' => '',
'char' => 500, // number of characters.
'more' => ' &hellip;'
];
// Parse args.
$args = wp_parse_args( $args, $defaults );
// Trim the content.
return substr( $args['content'], 0, $args['char'] ) . $args['more'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment