Documentation-Issue-Tracker icon indicating copy to clipboard operation
Documentation-Issue-Tracker copied to clipboard

[HelpHub] Feedback on Block Patterns

Open zzap opened this issue 1 year ago • 5 comments

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

zzap avatar Oct 04 '24 07:10 zzap

Heads up @WordPress/docs-issues-coordinators, we have a new issue open. Time to use 'em labels.

github-actions[bot] avatar Oct 04 '24 07:10 github-actions[bot]

/assign

karthick-murugan avatar Jan 31 '25 07:01 karthick-murugan

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 ❤️

github-actions[bot] avatar Jan 31 '25 07:01 github-actions[bot]

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

  1. Change $allowed_post_types:

Replace 'post', 'page' with the post types where you want the pattern to appear (e.g., 'product' for WooCommerce products).

  1. Change the Pattern Content:

Replace the content with your desired block pattern code.

karthick-murugan avatar Jan 31 '25 07:01 karthick-murugan

Heads up @docs-reviewers - the "[Status] Review" label was applied to this issue.

github-actions[bot] avatar Feb 05 '25 08:02 github-actions[bot]