Skip to content

Commit

Permalink
Posts, Post Types: Avoid a PHP warning when the_content() is called…
Browse files Browse the repository at this point in the history
… outside of the loop.

Fixes #47824.

git-svn-id: https://develop.svn.wordpress.org/trunk@46079 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 7, 2019
1 parent b545a56 commit 6905468
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function get_the_content( $more_link_text = null, $strip_teaser = false, $post =
return '';
}

if ( null === $post ) {
if ( null === $post && in_the_loop() ) {
$elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
} else {
$elements = generate_postdata( $_post );
Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/tests/post/getTheContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public function test_should_respect_pagination_of_inner_post() {

$this->assertSame( 'Bang', $found );
}

/**
* @ticket 47824
*/
public function test_should_fall_back_to_post_global_outside_of_the_loop() {
$GLOBALS['post'] = self::factory()->post->create( array( 'post_content' => 'Foo' ) );

$this->assertSame( 'Foo', get_the_content() );
}
}

0 comments on commit 6905468

Please sign in to comment.