# Block Bindings Source: Main Category

> Registers a block bindings source that shows a post's main category name, link or description in any paragraph, heading or button, for example as a label above titles in a Query Loop.

- Type: PHP
- Runs: Run everywhere
- Category: [Block Editor](https://wpide.com/library/category/gutenberg/)
- Tags: block-bindings, categories, query-loop
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/bindings-primary-category/

## Code

```php
add_action( 'init', function () {
    if ( ! function_exists( 'register_block_bindings_source' ) ) {
        return;
    }

    register_block_bindings_source( 'wpide/category', array(
        'label'              => 'Main category',
        'uses_context'       => array( 'postId' ),
        'get_value_callback' => function ( $args, $block ) {
            $post_id    = ! empty( $block->context['postId'] ) ? (int) $block->context['postId'] : get_the_ID();
            $categories = get_the_category( $post_id );
            if ( ! $categories ) {
                return null;
            }
            $category = $categories[0];

            switch ( $args['key'] ?? 'name' ) {
                case 'link':
                    return esc_url( get_category_link( $category ) );
                case 'description':
                    return esc_html( wp_strip_all_tags( category_description( $category ) ) );
                default:
                    return esc_html( $category->name );
            }
        },
    ) );
} );
```

## Notes

Needs WordPress 6.5+; bind with "source":"wpide/category" and args {"key":"name"} (also link, description). The first category alphabetically is used.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Block Bindings Source: Main Category" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
