Created
February 1, 2023 03:32
-
-
Save dlh01/6f4a121496ee7c8d213af8574bd08d1a to your computer and use it in GitHub Desktop.
Match Blocks example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Find all paragraph blocks in a post: | |
$grafs = \Alley\WP\match_blocks( $post, [ 'name' => 'core/paragraph' ] ); | |
// Include paragraphs in inner blocks: | |
$grafs = \Alley\WP\match_blocks( | |
$post, | |
[ | |
'flatten' => true, | |
'name' => 'core/paragraph', | |
] | |
); | |
// Get all paragraphs and headings: | |
$blocks = \Alley\WP\match_blocks( | |
'<!-- wp:paragraph -->…', | |
[ | |
'name' => [ 'core/heading', 'core/paragraph' ], | |
] | |
); | |
// Get all images credited to the Associated Press: | |
$images = \Alley\WP\match_blocks( | |
$post, | |
[ | |
'attrs' => [ | |
[ | |
'key' => 'credit', | |
'value' => '/(The )?Associated Press/i', | |
'operator' => 'REGEX', | |
], | |
[ | |
'key' => 'credit', | |
'value' => 'AP', | |
], | |
'relation' => 'OR', | |
], | |
'name' => 'core/image', | |
] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment