4

I have a block template for a custom post type.

When the WP Core column block added support for different widths, I tried setting up the template like so (I changed the post type to "post" here in case anyone else plans to try it out):

<?php
add_action('init', 'wpse_register_post_template');
function wpse_register_post_template() {
    $post_type_object = get_post_type_object('post');
    $post_type_object->template = array(
        array('core/columns', array(),
            array(
                array('core/column', array('width' => '33.33'), array(
                    array('core/paragraph', array()),
                )),
                array('core/column', array('width' => '66.66'), array(
                    array('core/paragraph', array()),
                )),
            )
        )
    );
}
?>

However, when I add a new post, this doesn't actually apply the column widths. They end up being two 50%-width columns, in the Editor and on the front end, even though the database shows the inner columns have the "width" attributes saved correctly.

It looks like this in the database -

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"33.33"} -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Left Col</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column {"width":"66.66"} -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Right Col</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

In a post where I manually added the Columns block in the Editor, so I was able to choose the 33% / 66% widths from the UI, it looks like WP added inline styles to the divs:

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":33.33} -->
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:paragraph -->
<p>Left col</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column {"width":66.66} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:paragraph -->
<p>Right col</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

Is there a way to update the CPT template so that the added blocks will have this same markup and thus have the different widths set on the columns right when I create a new CPT, rather than having to do it manually every time?

1 Answer 1

3
+100

Change column width value from string to number, like this

array('core/column', array('width' => 33.33)
1
  • 1
    Oh my gosh. I thought I had tried everything... but that was exactly it. Thank you for pointing that out!!
    – WebElaine
    Commented Jan 29, 2020 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.