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

The Post Title block does not display the blog page name within template outside query #52668

Open
likethegoddess opened this issue Jul 17, 2023 · 3 comments
Labels
[Block] Post Title Affects the Post Title Block [Type] Enhancement A suggestion for improvement.

Comments

@likethegoddess
Copy link

likethegoddess commented Jul 17, 2023

Description

On #22724, the Post Title block is designated as the equivalent to the single_post_title() tag. In classic theme dev, one use of the single_post_title() tag is to display the name of the blog page title before the loop on the index.php template.

I would expect that adding the Post Title block before the Query block on the index.html template would display the blog page title before the query results. Instead, nothing is displayed.

When I created a home.html template and added a Post Title Block before the Query block, the title of the first query result is displayed instead of the title of the page.

Step-by-step reproduction instructions

  1. Go to template > index.html within a block theme.
  2. Add <!-- wp:post-title /--> before

or

  1. Go to template > index.html within a block theme.
  2. Duplicate file and rename home.html.
  3. Add <!-- wp:post-title /--> before

Screenshots, screen recording, code snippet

FSE HTML template

<!-- wp:post-title { ... } /-->

<!-- wp:query {"query":{ ... } -->

Classic PHP template

<header>
	<h1 class="page-title">
		<?php single_post_title(); ?> 
	</h1>
</header>

<?php
if ( have_posts() ) :

Environment info

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@carolinan carolinan added the [Block] Post Title Affects the Post Title Block label Jul 17, 2023
@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Jul 18, 2023
@webmandesign
Copy link
Contributor

@likethegoddess
I came across the same issue. The only solution I could find was to set up the Post Title context postId to the actual blog page ID using PHP. However, this only worked for brief amount of time as it seem it is going to be removed (still works with current Gutenberg 16.2.1).

The only future-proof solution I use is to build a "Blog page title" pattern (in twentytwentythree/patterns/blog-page-title.php, for example) containing a code such as:

<?php
/**
 * Title: Blog page title
 * Slug: twentytwentythree/blog-page-title
 * Categories: header
 */

$blog_page_id = get_option( 'page_for_posts' );

?>

<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><?php

	if ( $blog_page_id ) {
		echo get_the_title( $blog_page_id );
	} else {
		echo esc_html_x( 'Blog', 'Fallback blog page title.', 'twentytwentythree' );
	}

?></h1>
<!-- /wp:heading -->

<?php if ( has_excerpt( $blog_page_id ) ) : ?>
<!-- wp:paragraph -->
<p><?php echo get_the_excerpt( $blog_page_id ); ?></p>
<!-- /wp:paragraph -->
<?php endif;

Then I insert the pattern into index.html template file using <!-- wp:pattern {"slug":"twentytwentythree/blog-page-title"} /-->.

@likethegoddess
Copy link
Author

Thanks, @webmandesign, that's exactly what I needed. That's a shame the context is being removed. There has to be some way to accomplish this going forward.

@twobyte
Copy link

twobyte commented Jun 6, 2024

I am trying to return parsed post_content outside the loop via the rest api for a dynamic modal, and requiring the $post object to be global appears to be a breaking issue. The render_block_context filter is used to inject the post_id, which works for the core/post-featured-image block, but not the core/post-title block.

Example code:

if ($post->post_content && $post->post_status === "publish") {
    $this->modal_post = $post;
    $blocks = parse_blocks($post->post_content);
    $parsed_blocks = '';
    add_filter('render_block_context', function($context, $parsed_block, $parent_block) { 
         // Update the $context variable.
        $context['postId'] = $this->modal_post->ID;
        $context['postType'] = $this->modal_post->post_type;
        return $context; 
    }, 15, 3);
    foreach( $blocks as $block ) {
        $parsed_blocks .= apply_filters('the_content', render_block( $block ) );
    }
    $response = new WP_REST_Response(['post_content' => $parsed_blocks]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Title Affects the Post Title Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

5 participants