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() );
1 time — 0.005081 sec (very slow) | 50000 times — 3.32 sec (fast) | PHP 7.2.5, WP 4.9.6
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
$post(WP_Post/object/int) (since 5.2)
Post, content of which we need to get.
Default: null
Examples
#1 Get the content of the post in to the variable
Note that this function does not return the same content as the_content() . To display the same content, you must pass it through the the_content() hook:
$content = apply_filters( 'the_content', get_the_content() );
echo $content;
#2 Display the post content, ending with “Read more” if needed
$content = get_the_content( 'Read more' );
echo apply_filters( 'the_content', $content );
#3 Display the post content only if it is not empty [auto-translate]
$the_content = apply_filters( 'the_content', get_the_content() );
if ( ! empty( $the_content ) ) {
echo $the_content;
}
For a separate post with ID 12:
$post = get_post( 12 ); // specific post
$the_content = apply_filters( 'the_content', $post->post_content );
if ( ! empty( $the_content ) ) {
echo $the_content;
}
Add Your Own Example
Notes
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.
Changelog
Since 0.71
Introduced.
Since 5.2.0
Added the $post parameter.
get_the_content() get the content code
WP 6.7.1
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 '';
}
/*
* Use the globals if the $post parameter was not specified,
* but only after they have been set up in setup_postdata().
*/
if ( null === $post && did_action( 'the_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: Post title. */
__( 'Continue reading %s' ),
the_title_attribute(
array(
'echo' => false,
'post' => $_post,
)
)
),
__( '(more…)' )
);
}
$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 the requested page doesn't exist.
if ( $elements['page'] > count( $elements['pages'] ) ) {
// Give them the highest numbered page that DOES exist.
$elements['page'] = count( $elements['pages'] );
}
$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 ( str_contains( $_post->post_content, '<!--noteaser-->' )
&& ( ! $elements['multipage'] || 1 === $elements['page'] )
) {
$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 );
}
}
return $output;
}
Related Functions