コード例 #1
0
/**
 * Get the HTML for the excerpt in the grid
 *
 * @access   public
 * @since    1.0.0
 * @param    $post_id Int - The ID of the post to get the excerpt of
 * @param    $word_limit Int - The total number of words to include in the excerpt
 * @param    $read_more_text String - The ID of the post to get the title of
 * @return   $html_output String - A string holding the html for an excerpt in the grid
 */
function mp_stacks_postgrid_excerpt($post_id, $word_limit, $read_more_text = NULL)
{
    $the_excerpt = mp_core_get_excerpt_by_id($post_id);
    //Check word limit for excerpt
    if (!empty($word_limit)) {
        //Cut the excerpt off at X number of words
        $the_excerpt = mp_core_limit_text_to_words($the_excerpt, $word_limit);
    }
    //If there are 0 words in this excerpt
    if (mp_core_word_count($the_excerpt) == 0) {
        return NULL;
    } else {
        $output_string = strip_tags($the_excerpt);
        $output_string .= !empty($read_more_text) ? '<span class="mp-stacks-postgrid-read-more">' . $read_more_text . '</span>' : NULL;
    }
    $postgrid_output = mp_stacks_grid_highlight_text_html(array('class_name' => 'mp-stacks-postgrid-item-excerpt', 'output_string' => $output_string));
    return $postgrid_output;
}
コード例 #2
0
/**
 * Limit number of words in a string
 *
 * @access   public
 * @since    1.0.0
 * @param    $text  string The string whose words we want to limit
 * @param    $limit int The number of words to limit the text to
 * @return   $text  string The limited string
 */
function mp_core_limit_text_to_words($text, $limit)
{
    if (mp_core_word_count($text) > $limit) {
        $words = str_word_count($text, 2);
        $pos = array_keys($words);
        $text = isset($pos[$limit]) ? substr($text, 0, $pos[$limit]) : $text;
    }
    return $text;
}