返回介绍

wp_trim_words()

发布于 2017-09-11 13:00:58 字数 4228 浏览 827 评论 0 收藏 0

wp_trim_words( string $text,  int $num_words = 55,  string $more = null )

Trims text to a certain number of words.


description

This function is localized. For languages that count ‘words’ by the individual character (such as East Asian languages), the $num_words argument will apply to the number of individual characters.


参数

$text

(string) (Required) Text to trim.

$num_words

(int) (Optional) Number of words.

Default value: 55

$more

(string) (Optional) What to append if $text needs to be trimmed. Default '…'.

Default value: null


返回值

(string) Trimmed text.


源代码

File: wp-includes/formatting.php

function wp_trim_words( $text, $num_words = 55, $more = null ) {
	if ( null === $more ) {
		$more = __( '…' );
	}

	$original_text = $text;
	$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 );
		$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
		$sep = '';
	} else {
		$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
		$sep = ' ';
	}

	if ( count( $words_array ) > $num_words ) {
		array_pop( $words_array );
		$text = implode( $sep, $words_array );
		$text = $text . $more;
	} else {
		$text = implode( $sep, $words_array );
	}

	/**
	 * Filters the text content after words have been trimmed.
	 *
	 * @since 3.3.0
	 *
	 * @param string $text          The trimmed text.
	 * @param int    $num_words     The number of words to trim the text to. Default 55.
	 * @param string $more          An optional string to append to the end of the trimmed text, e.g. ….
	 * @param string $original_text The text before it was trimmed.
	 */
	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}

更新日志

Versiondescription
3.3.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: _x()
  • wp-includes/formatting.php: wp_strip_all_tags()
  • wp-includes/formatting.php: wp_trim_words
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()
  • wp-admin/includes/dashboard.php: wp_dashboard_recent_drafts()
  • wp-includes/formatting.php: wp_trim_excerpt()
  • wp-includes/widgets.php: wp_widget_rss_output()
  • wp-includes/nav-menu.php: wp_setup_nav_menu_item()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    An example which strips formatting:

    
    <?php
    echo wp_trim_words( get_the_content(), 40, '...' );
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文