How to hide meta block(s) in certain post format

As in wordpress Block Editor HandbookI added a "meta block" to my theme, and it worked, function duke_blocks()

As in wordpress Block Editor Handbook

I added a "meta block" to my theme, and it worked,

    function duke_blocks() {

    wp_register_script(
        'wb-blocks-js',
        get_template_directory_uri() . '/inc/meta-block.js',
        array( 'wp-blocks', 'wp-editor', 'wp-element','wp-components' )
    );
    register_block_type( 'dukeyin/audio-url', array(
        'editor_script' => 'wb-blocks-js',
    ) );

}
add_action( 'init', 'duke_blocks' );

function duke_register_block_meta() {

    register_meta( 'post', 'audio-url', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
        'object_subtype' => 'audio',
    ) );

}

add_action( 'init', 'duke_register_block_meta' );

and js:

( function( wp ) {
    var el = wp.element.createElement;
    var registerBlockType = wp.blocks.registerBlockType;
    var TextControl = wpponents.TextControl;

    registerBlockType( 'dukeyin/audio-url', {
    title: 'Audio file URL',
    icon: 'media-audio',
    category: 'layout',
    attributes: {
        blockValue: {
            type: 'string',
            source: 'meta',
            meta: 'audio-url',
        }
    },

    edit: function( props ) {
        var setAttributes = props.setAttributes;

        function updateBlockValue( blockValue ) {
            setAttributes({ blockValue });
        }

        return el(
           'div',
           { className: "audio-url" },
            el( 'h5',{}, 'URL to audio file:'),
            el (TextControl,
            {
                onChange: updateBlockValue,
                value: props.attributes. blockValue,
            })
        );
    },

    save: function( props ) {
         return null;
    },
    } );
} )( window.wp );

but the porblem is: this is an "Audio file" meta block, I want to show it only when "Audio" post format is selected, when I select other post formats, I want to hide this block, how to do it?

Thank you!

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

相关推荐

  • How to hide meta block(s) in certain post format

    As in wordpress Block Editor HandbookI added a "meta block" to my theme, and it worked, function duke_blocks()

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信