get_the_content( string $more_link_text = null, bool $strip_teaser = false, WP_Post|object|int $post = null )

Retrieve the post content.


Description Description


Parameters Parameters

$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

$post

(WP_Post|object|int) (Optional) WP_Post instance or Post ID/object. Default is null.

Default value: null


Top ↑

Return Return

(string)


Top ↑

Source Source

File: wp-includes/post-template.php

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

	$_post = get_post( $post );

	if ( ! ( $_post instanceof WP_Post ) ) {
		return '';
	}

	if ( null === $post ) {
		$elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
	} else {
		$elements = generate_postdata( $_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,
						'post' => $_post,
					)
				)
			),
			__( '(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 ( $elements['page'] > count( $elements['pages'] ) ) { // if the requested page doesn't exist
		$elements['page'] = count( $elements['pages'] ); // give them the highest numbered page that DOES exist
	}

	$page_no = $elements['page'];
	$content = $elements['pages'][ $page_no - 1 ];
	if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
		if ( has_block( 'more', $content ) ) {
			// Remove the core/more block delimiters. They will be left over after $content is split up.
			$content = preg_replace( '/<!-- \/?wp:more(.*?) -->/', '', $content );
		}

		$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-->' ) && ( ! $elements['multipage'] || $elements['page'] == 1 ) ) {
		$strip_teaser = true;
	}

	$teaser = $content[0];

	if ( $elements['more'] && $strip_teaser && $has_teaser ) {
		$teaser = '';
	}

	$output .= $teaser;

	if ( count( $content ) > 1 ) {
		if ( $elements['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( $_post ) . "#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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
5.2.0 Added the $post parameter.
0.71 Introduced.

Top ↑

More Information More Information

When used inside The Loop, this function will get the content of the current post.

If used outside The Loop, you must inform the post you want to get the content from using the optional $post parameter.

An important difference from the_content() is that get_the_content() does not pass the content through the the_content filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.

 



Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 3 content
    Contributed by netzgestaltung

    find out if the_content has content before output

    in functions.php and similar files:

    // write inside the loop
    $the_content = apply_filters('the_content', get_the_content());
    if ( !empty($the_content) ) {
      echo $the_content;
    }
    // with post object by id
    $post = get_post(12); // specific post
    $the_content = apply_filters('the_content', $post->post_content);
    if ( !empty($the_content) ) {
      echo $the_content;
    }

    as function

    // call inside the loop or with ID
    // taken from has_excerpt() 
    // https://developer.wordpress.org/reference/functions/has_excerpt
    function mytheme_has_content( $post = 0 ){
      $post = get_post( $post );
      return ( !empty(apply_filters('the_content', $post->post_content)) );
    }

    template inside the loop:

    <?php if ( $customQuery->have_posts() ) {?>
      <?php while ( $customQuery->have_posts() ) {
        $customQuery->the_post(); ?>
        <?php $the_content = apply_filters('the_content', get_the_content()); ?>
        <!-- html -->
        <?php if ( !empty($the_content) ) { ?>
          <div class="content">
            <?php echo $the_content; ?>
          </div>
        <?php } ?>
      <?php } ?>
      <?php wp_reset_postdata(); ?>
    <?php } ?>

You must log in before being able to contribute a note or feedback.