Reduce Duplication — get_template_part()
Tujuan
Menghilangkan duplikasi kode event listing yang muncul di 4 tempat berbeda, menggunakan get_template_part().
Masalah
Kode HTML untuk menampilkan satu event (class event-summary) terduplikasi di:
front-page.php— Homepagearchive-event.php— All Events archivepage-past-events.php— Past Events pagesingle-program.php— Related events di program detail
Langkah 1: Buat Folder & File Template Part
- Buat folder baru di theme:
template-parts/ - Buat file baru:
template-parts/content-event.php - Pindahkan kode event HTML ke file ini:
php
<!-- template-parts/content-event.php -->
<div class="event-summary">
<a class="event-summary__date t---center" href="<?php the_permalink(); ?>">
<span class="event-summary__month">
<?php
$eventDate = new DateTime(get_field('event_date'));
echo $eventDate->format('M');
?>
</span>
<span class="event-summary__day"><?php echo $eventDate->format('d'); ?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h5>
<p><?php if (has_excerpt()) { echo get_the_excerpt(); } else { echo wp_trim_words(get_the_content(), 18); } ?>
<a href="<?php the_permalink(); ?>" class="nu gray">Learn more</a>
</p>
</div>
</div>Langkah 2: Gunakan get_template_part() di Template
Sintaks
php
get_template_part('folder/file-slug');⚠️ Tidak perlu menyertakan
.php— cukup slug saja.
Update front-page.php
php
<?php
while ($homepageEvents->have_posts()) {
$homepageEvents->the_post();
get_template_part('template-parts/content-event');
}
?>Update archive-event.php
php
<?php
while (have_posts()) {
the_post();
get_template_part('template-parts/content-event');
}
?>Update page-past-events.php
php
<?php
while ($pastEvents->have_posts()) {
$pastEvents->the_post();
get_template_part('template-parts/content-event');
}
?>Update single-program.php
php
<?php
while ($homepageEvents->have_posts()) {
$homepageEvents->the_post();
get_template_part('template-parts/content-event');
}
?>Argumen Kedua (Opsional): Specialty Name
get_template_part() menerima argumen kedua untuk nama spesialitas:
php
// Mencari file: template-parts/content-event.php
get_template_part('template-parts/content', 'event');
// Mencari file: template-parts/content-professor.php
get_template_part('template-parts/content', 'professor');
// Dinamis berdasarkan post type:
get_template_part('template-parts/content', get_post_type());
// → Jika post type = event → cari content-event.php
// → Jika post type = professor → cari content-professor.phpCara Kerja Penamaan
get_template_part('template-parts/content', 'event');
└── base slug ──────────┘ └ specialty ┘
↓
Cari file: template-parts/content-event.php
└─ dash ditambah otomatis💡 Argumen kedua berguna untuk halaman search results global, di mana setiap post type bisa ditampilkan dengan format berbeda.
Kapan Pakai Function vs get_template_part()?
| Kriteria | Custom Function (functions.php) | get_template_part() |
|---|---|---|
| Butuh argumen/parameter? | ✅ Ya | ❌ Tidak (kode selalu sama) |
| Contoh | pageBanner() — title, subtitle, photo berbeda | Event listing — HTML selalu identik |
| Fleksibilitas | Tinggi (bisa pass data) | Rendah (static blob) |
| Use case | Banner, custom output yang berubah-ubah | Repeated HTML blocks |
💡 Aturan praktis: Jika kode yang terduplikasi persis sama di semua lokasi (tidak butuh variasi), gunakan
get_template_part(). Jika butuh variasi/argumen, buat fungsi kustom.