WordPress Block Editor: Add a custom Query block variation with a Post Template
source link: https://gist.github.com/melissajclark/666a26c38ab5d6f241b64b987197ed73
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
WordPress Block Editor - Adding a Query Block Variation with nested InnerBlocks
As of October 29th, 2022, WordPress core does not have a Query block variation that includes the post's Categories. This code snippet fixes that.
Use this code to add a variation to the Query Block. The variation will appear when choosing a Post Template for the Query block.
The new variation will have the post's Featured Image, Title, and Categories. The title will be an H3 and link to the post. The featured image will use the Thumbnail size.
Steps to include this in your WordPress theme
If you don't have a Block Editor JS file yet
- Add the
functions.php
code to your functions.php file - If you don't have an Block Editor JS file yet: create a
block-editor.js
file in yourassets/js/
folder. Copy and paste theblock-editor.js
code included here into yourblock-editor.js
file.
If you already have a Block Editor JS file
- Paste in like 16-50 of the
block-editor.js
file included here. Put that inside yourwp.domReady( () => { } );
function. Put line 1-12 go before yourwp.domReady( () => {} );
function. Lines 1-12 allow us to use a WordPress Core icon for our block variation: imageDateTitle.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK