This repository has been archived on 2020-05-08. You can view files and clone it, but cannot push or open issues or pull requests.
understrap/inc/utilities.php

37 lines
958 B
PHP

<?php
/**
* Utility functions
*
* @package understrap
*/
/**
* Generate a custom length excerpt.
* If the content of the post is less than the provided length,
* the entire content is returned.
*
* @param int $post_id Post's ID.
* @param int $word_count How many words to keep.
*
* @return string
*/
function understrap_excerpt_with_length( $post_id, $word_count ) {
$post = get_post( $post_id );
$permalink = get_post_permalink( $post_id );
$content = strip_tags( $post->post_content );
if ( str_word_count( $content, 0 ) > $word_count ) {
$words = str_word_count( $content, 2 );
$keys = array_keys( $words );
$excerpt = substr( $content, 0, $keys[ $word_count ] );
$link_class = ' class=\"btn btn-secondary understrap-read-more-link\"';
$excerpt = '<p>' . $excerpt . '[...]</p>';
$excerpt .= '<p><a href="' . $permalink . '"' . $link_class . '>Read More</a></p>';
} else {
return $content;
}
return $excerpt;
}