Add Word Limit to Posts

时间:2012-01-04 作者:Howdy_McGee

有没有办法通过\\u content()函数或类似的函数来限制每篇文章的字数?

2 个回复
最合适的回答,由SO网友:Wyck 整理而成

您可以通过上的过滤器限制the_content

例如(在functions.php中):

add_filter("the_content", "Content_Filter_Custom");

  function Content_Filter_Custom($content)
  {
    // return a subset of it
    return substr($content, 0, 300); 
  }

SO网友:Chris_O

这里有一些我用来限制字数和字符数的过滤器。第一种是基于字符的,但只在单词之间拆分。

function nicetrim( $string ) {
        // limit the length of the given string to $MAX_LENGTH char
        // If it is more, it keeps the first $MAX_LENGTH-3 characters
        // and adds "..."
        // It counts HTML char such as á as 1 char.

        $MAX_LENGTH = 50;

        if ( strlen(  $string )  <= $MAX_LENGTH ) {
            return $string;
        }

        $s2  =  substr( $string, 0, $MAX_LENGTH  );
        $s3  = preg_split( "/\\s+(?=\\S*+$)/", $s2 );
        $s4 = $s3[0];
        $s4 .= "…";
        return $s4;
这是一个例外的字数变量,并查找您的more标记。它还可以在计数之前去掉任何html,然后再将其添加回来。

function trim_the_bio( $the_bio = \'\',  $all_words = 25, $more_link =\'\' ) {
        $the_bio = strange_chars ( $the_bio );
        $html = \'</span><a id="more-link" href="#">More &darr;</a><a style="display:none;" id="less-link" href="#">Less &uarr;</a>\';
        // Removes any tags not in the allowed tags array
        $allowed_tags = array ( \'a\', \'abbr\', \'b\', \'blockquote\', \'b\', \'cite\', \'code\', \'div\', \'em\', \'fon\', \'h1\', \'h2\', \'h3\', \'h4\', \'h5\', \'h6\', \'i\', \'img\', \'label\', \'i\', \'pre\', \'span\', \'strong\', \'title\', \'ul\', \'ol\', \'li\', \'object\', \'embed\', \'param\' );
        if ( $the_bio != \'\' && $all_words > 0 ) {
            // process allowed tags
            $allowed_tags = \'<\' . implode( \'><\', $allowed_tags ) . \'>\';
            $the_bio = str_replace( \' ]]>\', \' ]]>\', $the_bio );
            $the_bio = strip_tags( $the_bio, $allowed_tags );
            if ( 20 < count ( preg_split ( \'/[\\s]+/\', strip_tags ( $the_bio ), - 1 ) ) && $more_link != \'\' ) $flag = true; else $flag = false;
            // exclude HTML from counting words
            if ( $all_words > count( preg_split( \'/[\\s]+/\', strip_tags( $the_bio ), -1 ) ) )  if ( $flag == false ) { return $the_bio; } else { return $the_bio.$html; }
            // count all
            $all_chunks = preg_split( \'/([\\s]+)/\', $the_bio, -1, PREG_SPLIT_DELIM_CAPTURE );

            $the_bio = \'\';
            $count_words = 0;
            $enclosed_by_tag = false;
            foreach ( $all_chunks as $chunk ) {
                // is tag opened?
                if ( 0 < preg_match( \'/<[^>]*$/s\', $chunk ) ) $enclosed_by_tag = true;
                elseif ( 0 < preg_match( \'/>[^<]*$/s\', $chunk ) ) $enclosed_by_tag = false; // get entire word
                if( !$enclosed_by_tag && \'\' != trim( $chunk ) && substr( $chunk, -1, 1 ) != \'>\' ) $count_words ++;
                $the_bio .= $chunk;
                if ( $count_words >= $all_words && !$enclosed_by_tag ) break;
            }

            // Close any unclosed tags
            $the_bio = force_balance_tags( $the_bio );
        }

        // Throw it down...
       if ( $more_link != \'\' ) return $the_bio . $html;
       else return $the_bio;
    }

结束

相关推荐

AJAX search posts and pages

我正在尝试制作一个插件,在这里我需要创建一个AJAX搜索。Im使用中描述的方法http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/ 执行ajax调用。到目前为止一切都很好。问题是不确定如何在我的ajax函数中检索搜索结果。第一次尝试使用WP\\U查询,如下所示:$query= new WP_Query( array( \'post_type\'=> \'any\',\'post_status\' => \'publ