输出有限字数时忽略wordpress短代码


Ignore wordpress shortcode when outputting a limited word count

我在wordpress中创建了自己的自定义单词限制函数。我需要一种方法来忽略单词计数中的短代码我不想把它去掉,但忽略短代码作为字数的一部分。否则,如果你选择了一个数字,比如15,而短代码在15字限制的任何部分,那么页面将出现致命错误。

function my_word_limit($limit) {
    $content = explode(' ', get_the_content(), $limit);
            if (count($content)>=$limit) {
                    array_pop($content);
                    $content = implode(" ",$content).'...';
            } else {
                    $content = implode(" ",$content);
            }
    $content = apply_filters('the_content', $content);
    return $content;    
}

例如,这就是我要使用的短代码。

[di-video-logged-out]<iframe src="https://www.youtube.com/embed/LEIu8gba634" width="854" height="480"></iframe>[/di-video-logged-out]

用某个计数字修剪内容和限制

(即使内容被修剪,也可以在输出上保持短代码,如果不这样做,你也可以调整)

这里我的方法用wp_trim_word的内容和过滤器wp_trim_word。此外,您还可以将此函数wpso36236774_trim_words直接使用到$post->post_contentget_the_content中(无需过滤器)。代码中对用法进行了注释。

add_filter( 'wp_trim_words', 'wpso36236774_trim_words', 10, 4 );
/* Trims text to a certain number of words.
 * @author Jevuska
 * @version 1.0.1
 *
 * Kepp shortcode if exist in text.
 * Combination function of strip_shortcodes and wp_trim_words
 * Default $num_words = 55
 *
 ** USAGE
 ** Using directly
 ** wpso36236774_trim_words( $text, 56 )
 ** wpso36236774_trim_words( $text, 56, null, false, false, true ) - return array
 ** Shortcode hidden if $num_words is not set or if set with value = 55 with 4 arguments
 **
 ** Use wp_trim_words
 ** wp_trim_words( $text, $num_words = 56 )
 ** Fire wp_trim_words
 ** Shortcode hidden if $num_words is not set or $num_words = 55
 ** Position always in bottom
 ** add_filter( 'wp_trim_words', 'wpso36236774_trim_words', 10, 4 );
 *
 * @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. &hellip;.
 * @param  string  $original_content The text before it was trimmed.
 * @param  mix     $pos              Shortcode Position. You can set 'top' value if using directly
 * @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 wpso36236774_trim_words( $text, $num_words = 55, $more = null, $original_content = false, $pos = false, $count = false )
{
    if ( null === $more)
        $more = ' ' . '[&hellip;]';
    
    $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] ) && is_array( $match[0] ) )
            {
                $shortcode = '';
                $length    = count( $match[0] );
                for ( $i = 0 ; $i < $length; $i++ )
                    $shortcode .= do_shortcode( $match[0][ $i ] ); //match shortcode
            }
            
            $text = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $text );
            $text = unescape_invalid_shortcodes( $text );
        }
    }
    
    /* Hide shortcode
     * Base on count function arguments
     *
     */
    if ( func_num_args() == 1 || ( func_num_args() == 4 && 55 == $num_words ) )
         $shortcode = '';
     
    /* 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 .= $more;
        
        /* keep shortcode if exists and set position ( top or bottom text )
         *
         */
        switch( $pos )
        {
            case 'top' :
                $text = $shortcode . $text;
                break;
            
            default :
                $text .= $shortcode;
                break;
        }
    }
    else
    {
        $text = apply_filters( 'the_content', $original_content );
    }
    
    if ( $count )
        return array(
            'text'  => $text,
            'count' => $word_count
        );
    
    return $text; //output
}

如果有关于此代码的任何问题,请告诉我。根据需要调整,我希望这会有所帮助。

更新

  • 修复多个短代码
  • 基于总参数隐藏短代码选项
  • 修补$pos以获取顶部和底部文本的附加快捷代码位置(仅限函数)。对于另一个位置,您必须为您的短代码设置class和css