Hi @dahliasan,
This two functions do the same thing, block_header_area call the function block_template_part which call the function do_blocks
function block_header_area() {
block_template_part( 'header' );
}
function block_template_part( $part ) {
$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return;
}
echo do_blocks( $template_part->content );
}
Sorry, I should have elaborated a little. I haven’t tried, but I think something like this should work :
<html>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<?php
$block_header_part = get_block_template( get_stylesheet() . '//header', 'wp_template_part' );
$block_header = $block_header_part && ! empty( $block_header_part->content ) ? do_blocks( $block_header_part->content ) : '';
$block_content = do_blocks( '
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:post-content /-->
</div>
<!-- /wp:group -->'
);
?>
<?php wp_head(); ?>
</head>
<body>
<?php
echo $block_header;
echo $block_content;
?>
</body>
</html>
Can you test that and tell me if it’s work ?
Hi Nicolas, thanks so much. Yes it worked. This is what I did in the end. All I needed to do was not echo the code.
// header.php
<?php
function my_template_part( $part ) {
$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return;
}
do_blocks( $template_part->content );
}
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php my_template_part('header') ?>
<?php my_template_part('footer') ?>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<div class="wp-site-blocks">
<header class="wp-block-template-part site-header">
<?php block_header_area(); ?>
</header>
My header and footer is a big string and I’d have to manually update the code if I make any edits in FSE to the header and footer parts if I used your method in your second message which was what I was originally doing.
-
This reply was modified 1 year, 5 months ago by
dahliasan.
You’re welcome, thank for your feed back !