WordPress at a glance

get_the_content() WP 0.71

Gets the content of the current post. Must be used inside the WordPress loop.

Does not filter content as the_content() does. Therefore, if you want to get the result by applying all the_content hooks to the content, use this construction:

apply_filters( 'the_content', get_the_content() );
  • Global. Int. $page Page number of a single post/page.
  • Global. Int. $more Boolean indicator for whether single post/page is being viewed.
  • Global. true/false. $preview Whether post/page is in preview mode.
  • Global. Array. $pages Array of all pages in post/page. Each array element contains part of the content separated by the <!--nextpage--> tag.
  • Global. Int. $multipage Boolean indicator for whether multiple pages are in play.
✈ 1 time = 0.005081s = very slow | 50000 times = 3.32s = fast PHP 7.2.5, WP 4.9.6
Hooks from function:
Return

String. Post content.

Usage

$content = get_the_content( $more_link_text, $strip_teaser );
$more_link_text(string)
The text of the "read more" link. This link is added when the post content has html comment <!--more--> and when this function uses on any archive page (not single page).
Default: null
$strip_teaser(true/false)

Do we need strip content before the <!--more--> tag when the content is shown on singular page. The word "teaser" means attractive text before the <!--more--> tag.

By default this parameter is disabled. It can also be enabled by specifying anywhere in the post content <!--noteaser--> html comment (usually, it specified immediately after the <!--more-->).
Default: false

Examples

#1 Get the content of the post

Output the content of the post with the link "read more" where necessary:

<?php
$content = get_the_content('read more');
echo $content;
?>

Code of get_the_content: wp-includes/post-template.php VER 4.9.8

<?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;
}

Related Functions

From tag: WP Loop

More from category: Posts, pages...

No comments
    Hello, !     Log In . Register