Button Group


目录

Description

The Button Group field provides a neat UI for selecting a value.

Screenshots

Three examples of button group field controlling alignment, and colors of buttons

The Button Group field interface

List of field settings shown when setting up a Button Group field

The Button Group field settings

Changelog

Settings

Template usage

Display value

This example demonstrates how to display the selected value using the Return Format setting Value.

<p>Color: <?php the_field('color'); ?></p>

Display value and label

This example demonstrates how to display the selected value and label using the Return Format setting Both.

<?php
$color = get_field('color');
?>
<p>Color: <span class="color-<?php echo esc_attr($color['value']); ?>"><?php echo esc_html($color['label']); ?></span></p>

Conditional

This example demonstrates how to use a selected value to conditionally perform a task.

<?php 

if( get_field('color') == 'red' ) {
    // Do something.
}

Query Posts

This example demonstrates how to query posts that have a ‘red’ value for the field ‘color’.

<?php

$posts = get_posts( array(
    'meta_query' => array(
        array(
            'key'   => 'color',
            'value' => 'red',
        )
    )
) );

if( $posts ) {
    foreach( $posts as $post ) {
        // Do something.
    }
}

版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC