Skip to content

Commit

Permalink
Detect current post more reliably in core/post-title and core/post-co…
Browse files Browse the repository at this point in the history
…ntent renderers.
  • Loading branch information
felixarntz committed Sep 18, 2019
1 parent 6c99cca commit 1723b8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 9 additions & 5 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
/**
* Renders the `core/post-content` block on the server.
*
* @global WP_Query $wp_query WordPress query object.
*
* @return string Returns the filtered post content of the current post.
*/
function render_block_core_post_content() {
// TODO: Without this temporary fix, an infinite loop can occur.
if ( is_admin() || defined( 'REST_REQUEST' ) ) {
return '';
}
global $wp_query;

if ( ! in_the_loop() ) {
if ( isset( $wp_query ) && ! in_the_loop() ) {
rewind_posts();
the_post();
}

// Ensure no infinite loop occurs.
if ( 'wp_template' === get_post_type() ) {
return '';
}

return '<div class="entry-content">' . apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content() ) ) . '</div>';
}

Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
/**
* Renders the `core/post-title` block on the server.
*
* @global WP_Query $wp_query WordPress query object.
*
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
*/
function render_block_core_post_title() {
if ( ! in_the_loop() ) {
global $wp_query;

if ( isset( $wp_query ) && ! in_the_loop() ) {
rewind_posts();
the_post();
}

// Ensure no infinite loop occurs.
if ( 'wp_template' === get_post_type() ) {
return '';
}

return the_title( '<h1>', '</h1>', false );
}

Expand Down

0 comments on commit 1723b8f

Please sign in to comment.