Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check postId returns a published wp_template_part (#26734) #26812

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
function render_block_core_template_part( $attributes ) {
$content = null;

if ( ! empty( $attributes['postId'] ) && get_post_status( $attributes['postId'] ) ) {
// If we have a post ID and the post exists, which means this template part
// is user-customized, render the corresponding post content.
$content = get_post( $attributes['postId'] )->post_content;
if ( ! empty( $attributes['postId'] ) ) {
// If we have a post ID, and it's a published template part,
// then render the corresponding post content.
// Else treat this as an error.
$post = get_post( $attributes['postId'] );
if ( $post && ( 'wp_template_part' === $post->post_type ) ) {
$post_status = get_post_status( $attributes['postId'] );
if ( 'publish' === $post_status ) {
$content = $post->post_content;
}
}
} elseif ( isset( $attributes['theme'] ) && basename( wp_get_theme()->get_stylesheet() ) === $attributes['theme'] ) {
$template_part_query = new WP_Query(
array(
Expand Down Expand Up @@ -47,7 +54,7 @@ function render_block_core_template_part( $attributes ) {
}

if ( is_null( $content ) ) {
return 'Template Part Not Found';
return 'Template Part Not Found: ' . $attributes['slug'];
}

// Run through the actions that are typically taken on the_content.
Expand Down