返回介绍

get_the_content()

发布于 2017-09-11 00:33:53 字数 5137 浏览 1026 评论 0 收藏 0

get_the_content( string $more_link_text = null,  bool $strip_teaser = false )

Retrieve the post content.


description


参数

$more_link_text

(string) (Optional) Content for when there is more text.

Default value: null

$strip_teaser

(bool) (Optional) Strip teaser content before the more text. Default is false.

Default value: false


返回值

(string)


源代码

File: wp-includes/post-template.php

function get_the_content( $more_link_text = null, $strip_teaser = false ) {
	global $page, $more, $preview, $pages, $multipage;

	$post = get_post();

	if ( null === $more_link_text ) {
		$more_link_text = sprintf(
			'<span aria-label="%1$s">%2$s</span>',
			sprintf(
				/* translators: %s: Name of current post */
				__( 'Continue reading %s' ),
				the_title_attribute( array( 'echo' => false ) )
			),
			__( '(more&hellip;)' )
		);
	}

	$output = '';
	$has_teaser = false;

	// If post password required and it doesn't match the cookie.
	if ( post_password_required( $post ) )
		return get_the_password_form( $post );

	if ( $page > count( $pages ) ) // if the requested page doesn't exist
		$page = count( $pages ); // give them the highest numbered page that DOES exist

	$content = $pages[$page - 1];
	if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
		$content = explode( $matches[0], $content, 2 );
		if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
			$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );

		$has_teaser = true;
	} else {
		$content = array( $content );
	}

	if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
		$strip_teaser = true;

	$teaser = $content[0];

	if ( $more && $strip_teaser && $has_teaser )
		$teaser = '';

	$output .= $teaser;

	if ( count( $content ) > 1 ) {
		if ( $more ) {
			$output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
		} else {
			if ( ! empty( $more_link_text ) )

				/**
				 * Filters the Read More link text.
				 *
				 * @since 2.8.0
				 *
				 * @param string $more_link_element Read More link element.
				 * @param string $more_link_text    Read More text.
				 */
				$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
			$output = force_balance_tags( $output );
		}
	}

	if ( $preview ) // Preview fix for JavaScript bug with foreign languages.
		$output =	preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );

	return $output;
}

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/formatting.php: force_balance_tags()
  • wp-includes/l10n.php: __()
  • wp-includes/kses.php: wp_kses_no_null()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: get_the_password_form()
  • wp-includes/post-template.php: post_password_required()
  • wp-includes/post-template.php: the_content_more_link
  • wp-includes/post-template.php: the_title_attribute()
  • wp-includes/post.php: get_post()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/formatting.php: wp_trim_excerpt()
  • wp-includes/deprecated.php: the_content_rss()
  • wp-includes/feed.php: get_the_content_feed()
  • wp-includes/post-template.php: the_content()

User Contributed Notes

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

    Note that get_the_content doesn’t return the same thing as what the_content displays. For that you need to do this:

    
    $content = apply_filters( 'the_content', get_the_content() );
    echo $content;
    
  2. Basic Usage
    Display the post content, ending with “Read more” if needed

    
    $content = get_the_content( 'Read more' );
    echo apply_filters( 'the_content', $content );
    

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

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

发布评论

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