Kumpulan Resep Pola Field
Sistem field Blockstudio sederhana jika digunakan sendiri: tambahkan field ke blockstudio.attributes, lalu akses melalui $a di templat. Namun, blok nyata jarang hanya memiliki satu field. Biasanya ada grup pengaturan terkait, field yang ditampilkan atau disembunyikan berdasarkan field lain, repeater dengan struktur baris yang rumit, serta data yang perlu dapat dikueri di luar editor blok.
Panduan ini mengumpulkan resep praktis untuk pola field umum. Setiap resep menunjukkan konfigurasi block.json dan kode templat terkait. Salin langsung ke blok Anda dan sesuaikan seperlunya.
Field Kondisional
Pola yang paling umum: menampilkan atau menyembunyikan field berdasarkan nilai field lain. Ini menjaga editor tetap rapi dengan hanya menampilkan opsi yang relevan.
Toggle untuk menampilkan opsi
Blok CTA yang menampilkan pengaturan tambahan saat pengguna mengaktifkan tombol:
{
"name": "my-theme/cta",
"title": "Call to Action",
"blockstudio": {
"attributes": [
{ "id": "heading", "type": "text", "label": "Heading", "default": "Ready to get started?" },
{ "id": "description", "type": "textarea", "label": "Description" },
{ "id": "showButton", "type": "toggle", "label": "Show Button", "default": true },
{
"id": "buttonText",
"type": "text",
"label": "Button Text",
"default": "Get Started",
"conditions": [[{ "id": "showButton", "operator": "==", "value": true }]]
},
{
"id": "buttonUrl",
"type": "link",
"label": "Button Link",
"conditions": [[{ "id": "showButton", "operator": "==", "value": true }]]
}
]
}
}
Field teks tombol dan tautan hanya muncul saat toggle aktif. Di templat, periksa toggle sebelum merender:
<div useBlockProps>
<h2><?php echo esc_html( $a['heading'] ); ?></h2>
<?php if ( $a['description'] ) : ?>
<p><?php echo esc_html( $a['description'] ); ?></p>
<?php endif; ?>
<?php if ( $a['showButton'] && $a['buttonUrl'] ) : ?>
<a href="<?php echo esc_url( $a['buttonUrl']['url'] ); ?>">
<?php echo esc_html( $a['buttonText'] ); ?>
</a>
<?php endif; ?>
</div>
Pilihan untuk mengganti kumpulan field
Blok hero dengan jenis latar yang menentukan field mana yang muncul:
{
"name": "my-theme/hero",
"title": "Hero",
"blockstudio": {
"attributes": [
{ "id": "heading", "type": "text", "label": "Heading" },
{
"id": "bgType",
"type": "select",
"label": "Background Type",
"default": "color",
"options": [
{ "value": "color", "label": "Solid Color" },
{ "value": "gradient", "label": "Gradient" },
{ "value": "image", "label": "Image" },
{ "value": "video", "label": "Video" }
]
},
{
"id": "bgColor",
"type": "color",
"label": "Background Color",
"conditions": [[{ "id": "bgType", "operator": "==", "value": "color" }]]
},
{
"id": "bgGradient",
"type": "gradient",
"label": "Background Gradient",
"conditions": [[{ "id": "bgType", "operator": "==", "value": "gradient" }]]
},
{
"id": "bgImage",
"type": "files",
"label": "Background Image",
"multiple": false,
"conditions": [[{ "id": "bgType", "operator": "==", "value": "image" }]]
},
{
"id": "bgVideo",
"type": "files",
"label": "Background Video",
"multiple": false,
"allowedTypes": ["video"],
"conditions": [[{ "id": "bgType", "operator": "==", "value": "video" }]]
},
{
"id": "overlay",
"type": "range",
"label": "Overlay Opacity",
"default": 50,
"min": 0,
"max": 100,
"conditions": [[{ "id": "bgType", "operator": "!=", "value": "color" }]]
}
]
}
}
Field opasitas overlay menggunakan != agar muncul untuk semua jenis latar kecuali warna solid. Hanya satu field latar yang ditampilkan pada satu waktu. Field color mengembalikan array berisi value, name, dan slug warna, sehingga templat membaca ['value']:
<?php
$bg_style = match ( $a['bgType'] ) {
'color' => $a['bgColor'] ? "background-color: {$a['bgColor']['value']}" : '',
'gradient' => $a['bgGradient'] ? "background: {$a['bgGradient']}" : '',
'image' => $a['bgImage'] ? "background-image: url({$a['bgImage']['url']})" : '',
default => '',
};
?>
<div useBlockProps style="<?php echo esc_attr( $bg_style ); ?>">
<?php if ( $a['bgType'] === 'video' && $a['bgVideo'] ) : ?>
<video autoplay muted loop playsinline>
<source src="<?php echo esc_url( $a['bgVideo']['url'] ); ?>" />
</video>
<?php endif; ?>
<h1><?php echo esc_html( $a['heading'] ); ?></h1>
</div>
Kondisi OR
Tampilkan field saat salah satu dari beberapa kondisi terpenuhi. Setiap array bagian dalam adalah grup AND; array luar menggabungkannya dengan OR:
{
"id": "caption",
"type": "text",
"label": "Caption",
"conditions": [
[{ "id": "bgType", "operator": "==", "value": "image" }],
[{ "id": "bgType", "operator": "==", "value": "video" }]
]
}
Ini menampilkan field caption saat bgType bernilai “image” ATAU “video”.
Kondisi berantai
Beberapa kondisi dalam array bagian dalam yang sama menggunakan logika AND:
{
"id": "altText",
"type": "text",
"label": "Alt Text",
"conditions": [[
{ "id": "bgType", "operator": "==", "value": "image" },
{ "id": "bgImage", "operator": "!empty" }
]]
}
Field teks alt hanya muncul saat bgType bernilai “image” DAN sebuah gambar telah dipilih.
Kondisi global
Tampilkan field berdasarkan jenis postingan, ID postingan, atau peran pengguna, bukan berdasarkan nilai field lain:
{
"id": "seoTitle",
"type": "text",
"label": "SEO Title",
"conditions": [[{ "type": "postType", "operator": "==", "value": "page" }]]
}
Jenis kondisi global yang tersedia: postType, postId, userRole, userId.
Pengaturan Terorganisasi
Saat blok semakin kompleks, mengelompokkan field secara logis membuat panel inspector lebih mudah dikelola.
Grup untuk penataan visual
Grup membuat bagian yang dapat diciutkan di panel inspector. Grup anonim (tanpa id) hanya untuk tampilan; field di dalamnya diakses menggunakan ID langsung:
{
"name": "my-theme/card",
"title": "Card",
"blockstudio": {
"attributes": [
{
"type": "group",
"title": "Content",
"attributes": [
{ "id": "title", "type": "text", "label": "Title" },
{ "id": "description", "type": "textarea", "label": "Description" },
{ "id": "image", "type": "files", "label": "Image", "multiple": false }
]
},
{
"type": "group",
"title": "Link",
"attributes": [
{ "id": "url", "type": "link", "label": "URL" },
{ "id": "openInNewTab", "type": "toggle", "label": "Open in New Tab" }
]
},
{
"type": "group",
"title": "Appearance",
"attributes": [
{
"id": "style",
"type": "select",
"label": "Style",
"default": "elevated",
"options": [
{ "value": "elevated", "label": "Elevated" },
{ "value": "outlined", "label": "Outlined" },
{ "value": "flat", "label": "Flat" }
]
},
{ "id": "accentColor", "type": "color", "label": "Accent Color" }
]
}
]
}
}
<div useBlockProps>
<h3><?php echo esc_html( $a['title'] ); ?></h3>
<p><?php echo esc_html( $a['description'] ); ?></p>
</div>
Field diakses secara langsung: $a['title'], $a['style'], $a['accentColor']. Grup hanya memengaruhi tata letak panel inspector.
Grup bernama dengan bs_get_group
Jika grup memiliki id, ID field turunannya akan diberi awalan. Gunakan bs_get_group() untuk menghapus awalan tersebut:
{
"id": "author",
"type": "group",
"title": "Author",
"attributes": [
{ "id": "name", "type": "text", "label": "Name" },
{ "id": "role", "type": "text", "label": "Role" },
{ "id": "avatar", "type": "files", "label": "Avatar", "multiple": false }
]
}
Tanpa helper, field diakses sebagai $a['author_name'], $a['author_role'], $a['author_avatar']. Dengan helper:
<?php $author = bs_get_group( $a, 'author' ); ?>
<div class="author">
<?php if ( $author['avatar'] ) : ?>
<img src="<?php echo esc_url( $author['avatar']['url'] ); ?>" alt="" />
<?php endif; ?>
<p><?php echo esc_html( $author['name'] ); ?></p>
<p><?php echo esc_html( $author['role'] ); ?></p>
</div>
Grup bernama berguna saat nama field yang sama muncul di beberapa grup (e.g., author_name, dan company_name) atau saat Anda ingin meneruskan grup sebagai satu kesatuan ke templat parsial.
Tata letak grid dalam grup
Gunakan properti style untuk menata anak grup berdampingan:
{
"type": "group",
"title": "Dimensions",
"style": {
"display": "grid",
"grid-template-columns": "1fr 1fr",
"grid-gap": "16px"
},
"attributes": [
{ "id": "width", "type": "unit", "label": "Width", "default": "100%", "switch": false },
{ "id": "height", "type": "unit", "label": "Height", "default": "auto", "switch": false }
]
}
Dua field unit ditampilkan berdampingan di panel inspector, bukan bertumpuk. Ini cocok untuk pasangan dimensi, nilai margin/padding, atau dua field yang secara konsep berpasangan.
Tab untuk bagian utama
Saat blok memiliki banyak pengaturan, tab membuatnya lebih rapi daripada daftar grup yang panjang:
{
"type": "tabs",
"tabs": [
{
"title": "Content",
"attributes": [
{ "id": "title", "type": "text", "label": "Title" },
{ "id": "body", "type": "wysiwyg", "label": "Body" }
]
},
{
"title": "Media",
"attributes": [
{ "id": "image", "type": "files", "label": "Image", "multiple": false },
{ "id": "video", "type": "files", "label": "Video", "multiple": false, "allowedTypes": ["video"] }
]
},
{
"title": "Settings",
"attributes": [
{ "id": "layout", "type": "select", "label": "Layout", "options": [
{ "value": "left", "label": "Media Left" },
{ "value": "right", "label": "Media Right" }
]},
{ "id": "fullWidth", "type": "toggle", "label": "Full Width" }
]
}
]
}
Field pada setiap tab diakses seperti field tingkat atas: $a['title'], $a['image'], $a['layout']. Tab hanya berfungsi sebagai wadah UI.
Pola Repeater
Repeater memungkinkan pengguna menambahkan beberapa baris field. Repeater penting untuk daftar, grid, linimasa, dan komponen apa pun dengan jumlah item yang bervariasi.
Repeater dasar dengan batasan
Daftar fitur dengan 1 hingga 6 item:
{
"id": "features",
"type": "repeater",
"label": "Features",
"min": 1,
"max": 6,
"textButton": "Add Feature",
"attributes": [
{ "id": "icon", "type": "icon", "label": "Icon" },
{ "id": "title", "type": "text", "label": "Title", "default": "Feature" },
{ "id": "description", "type": "textarea", "label": "Description" }
]
}
<div useBlockProps class="grid grid-cols-3 gap-8">
<?php foreach ( $a['features'] as $feature ) : ?>
<div>
<?php if ( $feature['icon'] ) : ?>
<?php bs_render_icon( $feature['icon'] ); ?>
<?php endif; ?>
<h3><?php echo esc_html( $feature['title'] ); ?></h3>
<p><?php echo esc_html( $feature['description'] ); ?></p>
</div>
<?php endforeach; ?>
</div>
Label baris kustom
Secara default, baris repeater yang diciutkan menampilkan label umum. Gunakan textMinimized untuk menampilkan nilai field:
{
"id": "team",
"type": "repeater",
"label": "Team Members",
"textMinimized": { "id": "name", "fallback": "Team Member" },
"attributes": [
{ "id": "name", "type": "text", "label": "Name" },
{ "id": "role", "type": "text", "label": "Role" },
{ "id": "photo", "type": "files", "label": "Photo", "multiple": false }
]
}
Saat baris diciutkan, nama orang tersebut ditampilkan sebagai pengganti “Baris 1”. fallback digunakan saat field nama kosong. Anda juga dapat menambahkan string prefix dan suffix.
Kondisi di dalam repeater
Field di dalam repeater dapat merujuk field lain pada baris yang sama menggunakan id langsung (tanpa awalan):
{
"id": "links",
"type": "repeater",
"label": "Links",
"attributes": [
{ "id": "label", "type": "text", "label": "Label" },
{ "id": "url", "type": "link", "label": "URL" },
{
"id": "isExternal",
"type": "toggle",
"label": "External Link"
},
{
"id": "externalNote",
"type": "message",
"label": "Note",
"description": "External links open in a new tab with rel=\"noopener\".",
"conditions": [[{ "id": "isExternal", "operator": "==", "value": true }]]
}
]
}
Merujuk field luar dari dalam repeater
Saat kondisi di dalam repeater perlu memeriksa field di luar repeater, tambahkan "context": "main":
{
"id": "showDescriptions",
"type": "toggle",
"label": "Show Descriptions",
"default": true
},
{
"id": "items",
"type": "repeater",
"label": "Items",
"attributes": [
{ "id": "title", "type": "text", "label": "Title" },
{
"id": "description",
"type": "textarea",
"label": "Description",
"conditions": [[{
"id": "showDescriptions",
"operator": "==",
"value": true,
"context": "main"
}]]
}
]
}
Tanpa "context": "main", kondisi akan mencari field showDescriptions di dalam baris repeater. Dengan awalan itu, kondisi memeriksa toggle tingkat akar.
Tab di dalam repeater
Tab tidak tersedia di dalam repeater: field tabs di dalam baris repeater menampilkan pemberitahuan di editor, bukan field-nya, dan ekstensi PHPStan melaporkan hal tersebut. Sebaiknya susun field dalam baris secara datar. Untuk baris yang panjang, label baris kustom (textMinimized) membuat baris yang diciutkan tetap mudah dibaca, dan kondisi di dalam baris dapat menyembunyikan field yang hanya diperlukan oleh sebagian baris, seperti yang ditunjukkan dalam pola di atas.
Atur "minimized": true pada repeater agar baris yang sudah ada dimulai dalam keadaan diciutkan. Baris yang baru ditambahkan langsung terbuka. Pengecualian yang dibuka akan diingat di browser ini saat editor dimuat ulang; ini adalah preferensi pengeditan, bukan konten post yang disimpan. Repeater tanpa minimized tetap menggunakan perilaku bawaan yang membuat baris terbuka.
Pola Media
Satu gambar dengan fallback
{
"id": "image",
"type": "files",
"label": "Image",
"multiple": false
}
<?php if ( $a['image'] ) : ?>
<img
src="<?php echo esc_url( $a['image']['url'] ); ?>"
alt="<?php echo esc_attr( $a['image']['alt'] ); ?>"
<?php if ( isset( $a['image']['sizes']['full-width'], $a['image']['sizes']['full-height'] ) ) : ?>
width="<?php echo esc_attr( $a['image']['sizes']['full-width'] ); ?>"
height="<?php echo esc_attr( $a['image']['sizes']['full-height'] ); ?>"
<?php endif; ?>
/>
<?php endif; ?>
returnFormat default adalah "object", yang menyediakan ID, title, alt, caption, description, href, url, dan sizes. sizes menyimpan URL dari setiap ukuran gambar yang terdaftar (sizes['large']) beserta dimensinya (sizes['large-width'], sizes['large-height']). Lihat Atribut Rendering untuk daftar lengkap.
Galeri dengan batas jumlah
{
"id": "gallery",
"type": "files",
"label": "Gallery",
"multiple": true,
"max": 6,
"allowedTypes": ["image"]
}
<?php if ( $a['gallery'] ) : ?>
<div class="grid grid-cols-3 gap-4">
<?php foreach ( $a['gallery'] as $image ) : ?>
<img
src="<?php echo esc_url( $image['url'] ); ?>"
alt="<?php echo esc_attr( $image['alt'] ); ?>" />
<?php endforeach; ?>
</div>
<?php endif; ?>
Gambar yang dapat dinonaktifkan dengan switch
Properti switch menambahkan ikon mata agar pengguna dapat menonaktifkan field tanpa menghapus nilainya:
{
"id": "logo",
"type": "files",
"label": "Logo",
"multiple": false,
"switch": true
}
<?php
$is_disabled = ! empty( $a['_disabled'] ) && in_array( 'logo', $a['_disabled'], true );
if ( $a['logo'] && ! $is_disabled ) : ?>
<img src="<?php echo esc_url( $a['logo']['url'] ); ?>" alt="" />
<?php endif; ?>
Pengguna dapat menonaktifkan logo tanpa menghapus file yang diunggah. Mengaktifkannya kembali akan menampilkan file yang sama.
Variasi format pengembalian
Atur nilai yang dikembalikan oleh field files:
// Full object (default)
{ "id": "photo", "type": "files", "returnFormat": "object", "multiple": false }
// $a['photo']['url'], $a['photo']['alt'], $a['photo']['sizes']['full-width'], etc.
// Just the ID
{ "id": "photo", "type": "files", "returnFormat": "id", "multiple": false }
// $a['photo'] = 42
// Just the URL
{ "id": "photo", "type": "files", "returnFormat": "url", "multiple": false }
// $a['photo'] = "https://example.com/photo.jpg"
Gunakan "id" jika Anda perlu memanggil wp_get_attachment_image() sendiri. Gunakan "url" untuk gambar latar sederhana. Gunakan "object" (default) untuk kebutuhan lainnya.
Dengan "returnFormat": "id", referensi string yang diimpor seperti "buckshair-home" diteruskan tanpa perubahan. Editor menampilkan kartu pengganti tanpa gambar mini, termasuk di baris repeater. Membuka atau menyimpan halaman tidak mengubah referensi skalar menjadi array. Daftar campuran yang berisi ID lampiran dan referensi string juga berfungsi; hanya ID lampiran positif yang dicari di pustaka media.
Blockstudio tidak mencocokkan referensi string kustom dengan lampiran. Saat menggunakan nama portabel di berbagai situs, cocokkan sendiri di filter render atau templat. Memilih pengganti di pustaka media tetap menyimpan ID lampirannya seperti biasa.
Opsi Dinamis
Mengisi dari postingan
Field pilihan yang menampilkan semua halaman terpublikasi:
{
"id": "linkedPage",
"type": "select",
"label": "Linked Page",
"stylisedUi": true,
"populate": {
"type": "query",
"query": "posts",
"arguments": {
"post_type": "page",
"post_status": "publish",
"posts_per_page": -1
}
}
}
Field terisi secara otomatis dengan judul halaman sebagai label dan ID postingan sebagai nilai. Gunakan returnFormat di konfigurasi populate untuk menyesuaikan pemetaannya:
"populate": {
"type": "query",
"query": "posts",
"arguments": { "post_type": "page" },
"returnFormat": { "value": "ID", "label": "post_title" }
}
Mengisi dari istilah taksonomi
{
"id": "category",
"type": "select",
"label": "Category",
"stylisedUi": true,
"populate": {
"type": "query",
"query": "terms",
"arguments": {
"taxonomy": "category",
"hide_empty": true
}
}
}
Mengisi dari fungsi PHP
{
"id": "postType",
"type": "select",
"label": "Post Type",
"populate": {
"type": "function",
"function": "get_post_types",
"arguments": { "public": true }
}
}
Opsi statis dengan tambahan opsi terisi otomatis
Gabungkan opsi yang ditentukan langsung dengan opsi dinamis. Gunakan position untuk menentukan posisi opsi yang diisi otomatis:
{
"id": "source",
"type": "select",
"label": "Source",
"options": [
{ "value": "manual", "label": "Manual Entry" },
{ "value": "latest", "label": "Latest Posts" }
],
"populate": {
"type": "query",
"query": "posts",
"arguments": { "post_type": "page" },
"position": "after"
}
}
Opsi statis ditampilkan lebih dahulu, lalu diikuti halaman yang diisi secara dinamis.
Menyimpan Data untuk Kueri
Secara default, nilai field berada di dalam atribut blok pada post_content. Nilai tersebut tidak dapat dikueri dengan WP_Query. Jika perlu memfilter atau mengurutkan postingan berdasarkan nilai field, gunakan penyimpanan.
Post meta untuk field yang dapat dikueri
{
"id": "featured",
"type": "toggle",
"label": "Featured",
"storage": {
"type": "postMeta",
"postMetaKey": "is_featured"
}
},
{
"id": "priority",
"type": "number",
"label": "Priority",
"default": 0,
"min": 0,
"max": 100,
"storage": {
"type": "postMeta",
"postMetaKey": "priority_score"
}
}
Sekarang Anda dapat mengueri postingan unggulan:
$featured = new WP_Query( array(
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
),
),
'orderby' => 'meta_value_num',
'meta_key' => 'priority_score',
'order' => 'DESC',
) );
Kunci meta didaftarkan otomatis dengan show_in_rest, sehingga juga tersedia di REST API.
Penyimpanan ganda
Simpan di atribut blok dan post meta. Atribut blok menjaga nilai tetap tersedia di templat melalui $a, sedangkan post meta membuatnya dapat dikueri:
{
"id": "eventDate",
"type": "date",
"label": "Event Date",
"storage": {
"type": ["block", "postMeta"],
"postMetaKey": "event_date"
}
}
Penyimpanan opsi untuk nilai global
Untuk pengaturan seluruh situs yang harus sama di mana pun, gunakan penyimpanan opsi:
{
"id": "companyName",
"type": "text",
"label": "Company Name",
"storage": {
"type": "option",
"optionKey": "site_company_name"
}
}
Nilai disimpan di wp_options dan dapat diakses di mana saja dengan get_option('site_company_name'). Saat pengguna mengedit field ini di instans blok mana pun, nilainya diperbarui secara global.
Definisi Field yang Dapat Digunakan Kembali
Jika beberapa blok memiliki struktur field yang sama, definisikan sekali sebagai field kustom dan gunakan di mana saja.
Membuat field kustom
Buat field.json di direktori blockstudio/fields/ tema Anda:
theme/
└── blockstudio/
└── fields/
└── cta/
└── field.json
{
"name": "cta",
"title": "Call to Action",
"attributes": [
{ "id": "label", "type": "text", "label": "Button Label", "default": "Learn More" },
{ "id": "url", "type": "link", "label": "Button URL" },
{ "id": "style", "type": "select", "label": "Button Style", "default": "primary", "options": [
{ "value": "primary", "label": "Primary" },
{ "value": "secondary", "label": "Secondary" },
{ "value": "outline", "label": "Outline" }
]}
]
}
Menggunakan field kustom
Rujuk field tersebut di blok mana pun dengan "type": "custom/cta":
{
"blockstudio": {
"attributes": [
{ "id": "heading", "type": "text", "label": "Heading" },
{ "type": "custom/cta" }
]
}
}
Field CTA disertakan langsung ke dalam blok. Akses secara langsung: $a['label'], $a['url'], $a['style'].
Field kustom berawalan dengan idStructure
Saat menggunakan field kustom yang sama beberapa kali, tambahkan idStructure untuk memberi awalan pada ID:
{
"blockstudio": {
"attributes": [
{ "type": "custom/cta", "idStructure": "primary_{id}" },
{ "type": "custom/cta", "idStructure": "secondary_{id}" }
]
}
}
Field CTA pertama menjadi primary_label, primary_url, primary_style. Field kedua menjadi secondary_label, dan seterusnya.
Mengganti nilai default
Sesuaikan field tertentu saat merujuk ke field kustom:
{
"type": "custom/cta",
"idStructure": "hero_{id}",
"overrides": {
"label": { "default": "Get Started", "label": "CTA Label" },
"style": { "default": "primary" }
}
}
Penggantian digabungkan ke definisi asli. Anda dapat mengubah nilai default, label, kondisi, atau properti lainnya.
Pola Perenderan
Memeriksa nilai kosong
Sebagian besar jenis field mengembalikan false saat kosong. Selalu periksa sebelum merender:
<?php if ( $a['title'] ) : ?>
<h2><?php echo esc_html( $a['title'] ); ?></h2>
<?php endif; ?>
Untuk angka, 0 adalah nilai yang valid. Gunakan perbandingan ketat jika perlu membedakan “kosong” dan “nol”:
<?php if ( $a['count'] !== 0 || $a['count'] !== false ) : ?>
<span><?php echo esc_html( $a['count'] ); ?></span>
<?php endif; ?>
Pilihan dengan returnFormat: both
Saat Anda memerlukan nilai (untuk logika) sekaligus label (untuk ditampilkan):
{
"id": "status",
"type": "select",
"label": "Status",
"returnFormat": "both",
"options": [
{ "value": "active", "label": "Active" },
{ "value": "paused", "label": "On Hold" },
{ "value": "closed", "label": "Closed" }
]
}
<?php if ( $a['status'] ) : ?>
<span class="badge badge--<?php echo esc_attr( $a['status']['value'] ); ?>">
<?php echo esc_html( $a['status']['label'] ); ?>
</span>
<?php endif; ?>
Atribut data dari field attributes
Jenis field attributes memungkinkan pengguna menambahkan atribut data kustom:
{
"id": "dataAttrs",
"type": "attributes",
"label": "Data Attributes",
"link": true,
"media": true
}
<div useBlockProps <?php bs_render_data_attributes( $a['dataAttrs'] ); ?>>
Content here.
</div>
Variabel CSS dari nilai field
Buat properti kustom CSS dari field yang dipilih:
{
"id": "textColor", "type": "color", "label": "Text Color"
},
{
"id": "fontSize", "type": "unit", "label": "Font Size", "default": "1rem"
}
<div useBlockProps style="<?php bs_render_variables( $a, array( 'textColor', 'fontSize' ) ); ?>">
<p>Styled content.</p>
</div>
Ini menghasilkan style="--text-color: #333; --font-size: 1rem;" (ID field diubah menjadi kebab-case). Gunakan variabel tersebut di CSS Anda:
p {
color: var(--text-color);
font-size: var(--font-size);
}
Langkah Berikutnya
- Mendaftarkan atribut: properti
id,type,label,default,help,position, danconditions. - Referensi jenis field: referensi yang dibuat otomatis dengan setiap properti untuk setiap jenis field.
- Logika kondisional: operator, kondisi global, filter kondisi kustom, dan konteks repeater.
- Mengisi opsi: mode kueri, fungsi, kustom, dan fetch untuk field opsi dinamis.
- Field kustom:
field.json, jalur penemuan, filterblockstudio/fields, dan polaidStructurelanjutan. - Perenderan:
$a,$b,$c, nilai kosong, dan fungsi helper.