Use page Title in Gutenberg custom banner block

I have created a custom banner image block for Gutenberg, which works great, butI want to know if it is possible to use

I have created a custom banner image block for Gutenberg, which works great, but I want to know if it is possible to use the page title as the current banner text placeholder until it has been edited?

My Edit function is

 return [
            el('div', {className:'header-banner'},
                el(
                    element.Fragment,
                    null,
                    controls,
                    el( "div",{
                        className: 'banner-image',
                        style: { backgroundImage: 'url('+attributes.mediaURL+')' }
                    },
                    attributes.title || isSelected ?  el(RichText, {
                            key: 'editable',
                            tagName: "h1",
                            className: "banner-title",
                            //Can i add the page title in here if it is avaiable??
                            //placeholder: i18n.__('Write title…'),
                            value: attributes.title,
                            onChange: function onChange(value) {
                                return props.setAttributes({ title: value });
                            },
                            inlineToolbar: true
                        }) : null 

                    )
                )
            )//header-banner
        ];    

Thanks :)

I have created a custom banner image block for Gutenberg, which works great, but I want to know if it is possible to use the page title as the current banner text placeholder until it has been edited?

My Edit function is

 return [
            el('div', {className:'header-banner'},
                el(
                    element.Fragment,
                    null,
                    controls,
                    el( "div",{
                        className: 'banner-image',
                        style: { backgroundImage: 'url('+attributes.mediaURL+')' }
                    },
                    attributes.title || isSelected ?  el(RichText, {
                            key: 'editable',
                            tagName: "h1",
                            className: "banner-title",
                            //Can i add the page title in here if it is avaiable??
                            //placeholder: i18n.__('Write title…'),
                            value: attributes.title,
                            onChange: function onChange(value) {
                                return props.setAttributes({ title: value });
                            },
                            inlineToolbar: true
                        }) : null 

                    )
                )
            )//header-banner
        ];    

Thanks :)

Share Improve this question asked Aug 6, 2018 at 12:20 DesignMonkeyJimDesignMonkeyJim 3493 silver badges9 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 4

Due to the getDocumentTitle selector being deprecated as mentioned here https://stackoverflow/questions/51674293/use-page-title-in-gutenberg-custom-banner-block/51792096#comment92130728_51792096

I managed to get it working with a slight tweak to the code by Jim-miraidev

var GetTitle = function GetTitle(props) {
    return el("h1", {className: "jab-banner-title"}, props.title);
};

var selectTitle = withSelect(function (select) {
    var title;

    if (typeof select("core/editor").getPostEdits().title !== 'undefined') {
        title = select("core/editor").getPostEdits().title;
    } else {
        title = select("core/editor").getCurrentPost().title;
    }

    return {
        title: title
    };
});
var PostTitle = selectTitle(GetTitle);

.....

return [
    el('div', {className:'jab-header-banner '+classes+''},
        el(
            element.Fragment,
            null,
            controls,
            el( "div",{
                className: 'jab-banner-image',
                style: { backgroundImage: 'url('+attributes.mediaURL+')' }
            },
            el(PostTitle,{className: "jab-banner-title"})
            )
        )
    )//header-banner
]; 

Thanks to the answer in this post i have managed to add the title into the banner and it updates as the post title is being updated.

https://stackoverflow/questions/51674293/use-page-title-in-gutenberg-custom-banner-block/51792096#51792096

var withSelect  = wp.data.withSelect;

var GetTitle = function GetTitle(props) {
      return el("h1",{className: "jab-banner-title"},props.title);
};

var selectTitle = withSelect(function (select) {
     return {
            title: select("core/editor").getDocumentTitle()
     };
});
var PostTitle = selectTitle(GetTitle);

.....

 return [
            el('div', {className:'jab-header-banner '+classes+''},
                el(
                    element.Fragment,
                    null,
                    controls,
                    el( "div",{
                        className: 'jab-banner-image',
                        style: { backgroundImage: 'url('+attributes.mediaURL+')' }
                    },
                    el(PostTitle,{className: "jab-banner-title"})
                    )
                )
            )//header-banner
        ]; 

This worked for me

let title = typeof select("core/editor").getPostEdits().title !== 'undefined' ? select("core/editor").getPostEdits().title : select("core/editor").getCurrentPost().title;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744699707a4588706.html

相关推荐

  • Use page Title in Gutenberg custom banner block

    I have created a custom banner image block for Gutenberg, which works great, butI want to know if it is possible to use

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信