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
Raw Permalink Normal View History

2016-11-17 21:18:39 +00:00
<?php
/**
* Utility functions
2016-11-21 17:12:36 +00:00
*
* @package understrap
2016-11-17 21:18:39 +00:00
*/
/**
* Generate a custom length excerpt.
* If the content of the post is less than the provided length,
* the entire content is returned.
2016-11-21 17:12:36 +00:00
*
* @param int $post_id Post's ID.
* @param int $word_count How many words to keep.
2016-11-17 21:18:39 +00:00
*
* @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 ] );
2016-11-21 17:12:36 +00:00
$link_class = ' class=\"btn btn-secondary understrap-read-more-link\"';
2016-11-17 21:18:39 +00:00
$excerpt = '<p>' . $excerpt . '[...]</p>';
$excerpt .= '<p><a href="' . $permalink . '"' . $link_class . '>Read More</a></p>';
} else {
return $content;
}
return $excerpt;
}