[HelpHub] Feedback on Block Patterns
URL of the Page with the Issue
https://wordpress.org/documentation/article/block-pattern/
Originally reported by
https://profiles.wordpress.org/brnggncy/
Report content (issue description)
Is there a way to limit patterns to specific post types?
Section of Page with the issue
Why is this a problem?
Suggested Fix
Heads up @WordPress/docs-issues-coordinators, we have a new issue open. Time to use 'em labels.
/assign
Hey @karthick-murugan, thanks for your interest in this issue! 🍪🍪🍪
If you have any questions, do not hesitate to ask them in our #docs Slack channel.
Enjoy and happy contributing ❤️
Step 1: Add This Code to Your Theme’s functions.php File
Copy and paste the following code into your theme’s functions.php file or a custom plugin:
function mytheme_limit_pattern_to_post_types() {
// Define the post types where the pattern should appear
$allowed_post_types = array( 'post', 'page' ); // Change these to your desired post types
// Get the current post type
$current_post_type = get_post_type();
// Check if the current post type is allowed
if ( in_array( $current_post_type, $allowed_post_types, true ) ) {
// Register the pattern only for allowed post types
register_block_pattern(
'mytheme/custom-pattern',
array(
'title' => __( 'Custom Pattern', 'mytheme' ),
'description' => __( 'A custom pattern for specific post types.', 'mytheme' ),
'content' => '<!-- wp:paragraph --><p>This is a custom pattern limited to specific post types.</p><!-- /wp:paragraph -->',
'categories' => array( 'text' ),
)
);
}
}
add_action( 'init', 'mytheme_limit_pattern_to_post_types' );
Step 2: Customize the Code
- Change $allowed_post_types:
Replace 'post', 'page' with the post types where you want the pattern to appear (e.g., 'product' for WooCommerce products).
- Change the Pattern Content:
Replace the content with your desired block pattern code.
Heads up @docs-reviewers - the "[Status] Review" label was applied to this issue.