Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter nitrospectide

    (@nitrospectide)

    Can anyone clarify what I might be getting wrong here?

    I think your complete code would be useful if one should be able to understand this. You can make it available, for example, at https://gist.github.com.

    Thread Starter nitrospectide

    (@nitrospectide)

    @threadi Here are the contents for my variations.js file, which I pull in via enqueuing in functions.php:

    wp.domReady( function() {
    
        const MY_VARIATION_NAME = 'my-plugin/services-list';
    
        registerBlockVariation( 'core/query', {
            name: MY_VARIATION_NAME,
            title: 'Services List',
            description: 'Displays a list of Services',
            isActive: ( { namespace, query } ) => {
                return (
                    namespace === MY_VARIATION_NAME
                    && query.postType === 'services'
                );
            },
            icon: /** An SVG icon can go here*/,
            attributes: {
                namespace: MY_VARIATION_NAME,
                query: {
                    perPage: 6,
                    pages: 0,
                    offset: 0,
                    postType: 'services',
                    order: 'desc',
                    orderBy: 'date',
                    author: '',
                    search: '',
                    exclude: [],
                    sticky: '',
                    inherit: false,
                },
            },
            scope: [ 'inserter' ],
            }
        );
    
    
    });

    As mentioned in my original post, the enqueuing is working, and I can create other block variations. But I will include my enqueuing code for completeness:

    function enqueue_block_variations() {
    	wp_enqueue_script( 
    		'my-block-variations', // handle for script once loaded
    		get_stylesheet_directory_uri() . '/js/variations.js', 
    		array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ) // loads dependencies
    	);
    }
    add_action( 'enqueue_block_editor_assets', 'enqueue_block_variations' );
    Thread Starter nitrospectide

    (@nitrospectide)

    Is this functionality not yet in widespread use?

    threadi

    (@threadi)

    No, I think that’s been used rather little so far. Moreover, you have rather few developers here in the forum who deal with it. I think you’d be better off with Stackoverflow or the Gutenberg team.

    Looking at your request, I see 2 problems in your code:

    a) The specification of the icon is incorrect. This leads to a JavaScript error. If you don’t have an icon, I think you can leave it out.
    b) In the JavaScript console, you should see that “registerBlockVariation” is not defined when loading the block editor. To complete this, add

    const { registerBlockVariation } = wp.blocks;

    at the beginning of your variations.js file.

    After that it should work. In my test, at least, it works.

    Thread Starter nitrospectide

    (@nitrospectide)

    I somehow missed when your reply came in via email. Thank you! This fixes it. ??

    threadi

    (@threadi)

    Nice if I could help. You are welcome to set the topic to solved.

    Thread Starter nitrospectide

    (@nitrospectide)

    Done

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Query Loop Block Variation not working’ is closed to new replies.