Jika Anda menggunakan Shortcode dalam postingan, terkadang untuk medapatkan jumlah dan membatasi kata dalam postingan WordPress dengan tepat, kita agak kesulitan. Berikut ini adalah function yang bisa Anda tambahkan dalam file functions.php theme Anda, dalam kode sudah diberikan penjelasan dan cara penggunaan.
add_filter( 'wp_trim_words', 'jv_trim_words', 10, 5 );
/* Trims text to a certain number of words.
*
* Keep shortcode if exist in text.
* Combination function of strip_shortcodes and wp_trim_words
*
** USAGE
** Using directly
** jv_trim_words( $text, $num_words = 55, $more = null, $original_content = false, $count = false )
**
** Use wp_trim_words
** wp_trim_words( $text, $num_words = 55, $more = null )
** Fire wp_trim_words
** add_filter( 'wp_trim_words', 'jv_trim_words', 10, 5 );
*
* @param string $text Text to trim.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. ….
* @param string $original_content The text before it was trimmed.
* @param boolean $count Get word count
* @return string The text after the filter witch $num_words
* @return array If using directly and parameter $count set to true
*/
function jv_trim_words( $text, $num_words, $more = null, $original_content = false, $count = false )
{
if ( null === $more)
$more = ' ' . '[…]';
$shortcode = $strip_shortcode = true;
if ( ! $original_content )
$original_content = $text;
$text = $original_content;
/* Check existing shortcode
*
*/
if ( false === strpos( $text, '[' ) )
$strip_shortcode = false;
global $shortcode_tags;
if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) )
$strip_shortcode = false;
/* Strip content from shortcode
*
*/
if ( $strip_shortcode )
{
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $text, $matches );
$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
if ( ! empty( $tagnames ) )
{
$text = do_shortcodes_in_html_tags( $text, true, $tagnames );
$pattern = get_shortcode_regex( $tagnames );
preg_match_all( "/$pattern/", $text, $match );
if ( ! empty( $match[0][0] ) )
$shortcode = do_shortcode( $match[0][0] ); //match shortcode
$text = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $text );
$text = unescape_invalid_shortcodes( $text );
}
}
/* Split content into array words
*
*/
$text = wp_strip_all_tags( $text );
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) )
{
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$limit_words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$full_words_array = $words_array[0];
$sep = '';
}
else
{
$limit_words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$full_words_array = explode( ' ', preg_replace( "/[\n\r\t ]+/", ' ', $text ) );
$sep = ' ';
}
/* Check word count base on $num_words
*
*/
$word_count = count( $full_words_array );
if ( $word_count >= $num_words )
{
array_pop( $limit_words_array );
$text = implode( $sep, $limit_words_array );
$text = $text . $more . $shortcode; //keep shortcode if exists
}
else
{
$text = apply_filters( 'the_content', $original_content );
}
if ( $count )
return array(
'text' => $text,
'count' => $word_count
);
return $text; //output
}
Cara kerja function ini sama dengan function the_excerpt atau get_the_excerpt, hanya saja kita tetap menampilkan shortcode ketika postingan melebihi batas kata yang kita tentukan. Kode shortcode dan kode html tidak dihitung.
Share