Timber\Helper::trim_words PHP Метод

trim_words() публичный статический Метод

См. также: TextHelper::trim_words
Устаревший: since 1.2.0
public static trim_words ( string $text, integer $num_words = 55, string | null | false $more = null, string $allowed_tags = 'p a span b i br blockquote' ) : string
$text string
$num_words integer
$more string | null | false text to appear in "Read more...". Null to use default, false to hide
$allowed_tags string
Результат string
    public static function trim_words($text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote')
    {
        return TextHelper::trim_words($text, $num_words, $more, $allowed_tags);
    }

Usage Example

Пример #1
0
 /**
  * get a preview of your post, if you have an excerpt it will use that,
  * otherwise it will pull from the post_content.
  * If there's a <!-- more --> tag it will use that to mark where to pull through.
  * @api
  * @example
  * ```twig
  * <p>{{post.get_preview(50)}}</p>
  * ```
  * @param int $len The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content
  * @param bool $force What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length
  * @param string $readmore The text you want to use on the 'readmore' link
  * @param bool|string $strip true for default, false for none, string for list of custom attributes
  * @param string $end The text to end the preview with (defaults to ...)
  * @return string of the post preview
  */
 function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '&hellip;')
 {
     $text = '';
     $trimmed = false;
     if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
         if ($force) {
             $text = Helper::trim_words($this->post_excerpt, $len, false);
             $trimmed = true;
         } else {
             $text = $this->post_excerpt;
         }
     }
     if (!strlen($text) && preg_match('/<!--\\s?more(.*?)?-->/', $this->post_content, $readmore_matches)) {
         $pieces = explode($readmore_matches[0], $this->post_content);
         $text = $pieces[0];
         if ($force) {
             $text = Helper::trim_words($text, $len, false);
             $trimmed = true;
         }
         $text = do_shortcode($text);
     }
     if (!strlen($text)) {
         $text = Helper::trim_words($this->get_content(), $len, false);
         $trimmed = true;
     }
     if (!strlen(trim($text))) {
         return trim($text);
     }
     if ($strip) {
         $allowable_tags = is_string($strip) ? $strip : null;
         $text = trim(strip_tags($text, $allowable_tags));
     }
     if (strlen($text)) {
         $text = trim($text);
         $last = $text[strlen($text) - 1];
         if ($last != '.' && $trimmed) {
             $text .= $end;
         }
         if (!$strip) {
             $last_p_tag = strrpos($text, '</p>');
             if ($last_p_tag !== false) {
                 $text = substr($text, 0, $last_p_tag);
             }
             if ($last != '.' && $trimmed) {
                 $text .= $end . ' ';
             }
         }
         $read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more");
         if ($readmore && isset($readmore_matches) && !empty($readmore_matches[1])) {
             $text .= ' <a href="' . $this->link() . '" class="' . $read_more_class . '">' . trim($readmore_matches[1]) . '</a>';
         } elseif ($readmore) {
             $text .= ' <a href="' . $this->link() . '" class="' . $read_more_class . '">' . trim($readmore) . '</a>';
         }
         if (!$strip && $last_p_tag && (strpos($text, '<p>') || strpos($text, '<p '))) {
             $text .= '</p>';
         }
     }
     return trim($text);
 }
All Usage Examples Of Timber\Helper::trim_words