Skip to content

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:

  1. front-page.php — Homepage
  2. archive-event.php — All Events archive
  3. page-past-events.php — Past Events page
  4. single-program.php — Related events di program detail

Langkah 1: Buat Folder & File Template Part

  1. Buat folder baru di theme: template-parts/
  2. Buat file baru: template-parts/content-event.php
  3. 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.php

Cara 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()?

KriteriaCustom Function (functions.php)get_template_part()
Butuh argumen/parameter?✅ Ya❌ Tidak (kode selalu sama)
ContohpageBanner() — title, subtitle, photo berbedaEvent listing — HTML selalu identik
FleksibilitasTinggi (bisa pass data)Rendah (static blob)
Use caseBanner, custom output yang berubah-ubahRepeated 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.