Membuat template post file di WordPress
Pada Wordpress sendiri terdapat template post file untuk menampilkan postingan pada website. berikut
Index.php
template file index.php
digunakan untuk menampilkan postingan apabila tidak ada template file yang ditemukan. Setiap tema harus memiliki index.php agar tema valid.
Seringkali Anda menginginkan struktur atau tata letak konten yang unik. Ada banyak template yang bisa Anda gunakan untuk mengkostomisasi struktur konten berdasarkan konteks di dalam situs.
Dua template file posting yang paling terkenal adalah home.php
dan single.php
yang masing-masingnya menampilkan feed posting dan satu posting.
Berikut contoh template file index.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Home.php
Secara default, WordPress menyetel home atau beranda disitus Anda untuk menampilkan posting blog terbaru. Template file home.php
digunakan untuk membuat indeks posting blog.
Penggunaan template ini lebih dianjurkan daripada membuat template halaman kustom karena pagination blog pada template halaman kustom tidak akan berfungsi dengan baik. Jika home.php
tidak ada, WordPress akan menggunakan index.php
.
Berikut contoh template file home.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<header>
<h1>Home</h1>
</header>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Single.php
Template file single.php
digunakan untuk menampilkan satu posting tunggal.
Membuat sesederhana mungkin dalam struktur template dan tidak membuat lebih banyak template kecuali Anda benar-benar membutuhkannya. Oleh karena itu, sebagian besar developer WordPress tema tidak membuat file single-post.php karena single.php cukup spesifik.
Untuk sebagian besar tema harus memiliki file single.php
.
Berikut contoh template file single.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h1 class="title">', '</h1>' ); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Singular.php
Template file singular.php
muncul dalam hierarki setelah single.php
dan page.php
dan digunakan untuk posting tunggal apa pun tipe postingannya.
Berikut contoh template file singular.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h1 class="title">', '</h1>' ); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Archive.php
Ketika pengunjung web mengklik data meta, maka WordPress akan menggunakan template file archive.php
dan menampilkan postingan apa pun yang terkait dengan meta data yang tadi di klik.
Misalnya, jika pengunjung mengklik nama penulis, archive.php
akan menampilkan semua postingan si penulis tersebut.
Berikut contoh template file archive.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<header class="archive">
<?php the_archive_title( '<h1 class="archive-title">', '</h1>' ); ?>
<?php the_archive_description( '<div class="archive-description"><p>', '</p></div>' ); ?>
</header>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Author.php dan Date.php
Template file author.php
dan date.php
jenis file archive yang lebih spesifik. Jika Anda ingin membuat tata letak yang berbeda Anda bisa menggunakan template file yang lebih spesifik seperti author.php dan date.php.
Umumnya, template file archive.php
sudah cukup untuk sebagian besar kebutuhan tema dalam menampilkan data archive.
Author.php
Jika Anda membuat tema yang dirancang untuk banyak penulis, mungkin masuk akal untuk membuat template author.php
.
Dalam template file author.php
, Anda dapat memberikan lebih banyak informasi tentang penulis seperti menampilkan: gravatar, biografi, link social media, dan kemudian semua postingan yang ditulis oleh mereka.
Selain itu, Anda dapat membuat template file author.php
khusus untuk masing-masing penulis dengan menggunakan ID penulis atau nama penulis mereka.
Anda mungkin ingin semua informasi penulis ditampilkan dengan author.php tetapi Anda dapat membuat halaman penulis yang lebih spesifik misalnya penulis Yuky Hendiawan dengan membuat template file author-yukyhendiawan.php
atau author-15.php
jika ID penulisnya adalah 15.
Berikut contoh template file author.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<header class="author">
<h4>
<?php $author = esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) );
echo '<a href="' . $author . '">' . esc_html(get_the_author()) . '</a>'; ?>
</h4>
<p><?php echo esc_html( get_the_author_meta( 'description' ) ); ?></p>
</header>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Date.php
Jika Anda membuat tema yang dirancang untuk majalah atau situs web berita, mungkin masuk akal untuk mengatur template file date.php
karena situs web ini sering kali mengatur artikel dan posnya menurut tanggal atau terbitan.
Berikut contoh template file date.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<header class="date">
<?php $year = get_the_time('Y'); ?>
<?php $month = get_the_time('m'); ?>
<a href="<?php echo esc_url(get_month_link( $year, $month )); ?>">Bulan <?php the_time('F'); ?> Tahun <?php the_time('Y'); ?></a>
</header>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
Search.php
Jika Anda membuat tema dan didalamnya banyak berbagai macam artikel atau postingan, mungkin masuk akal untuk membuat template file search.php
. Sehingga mereka bisa menavigasi dengan menu pencarian.
Berikut contoh template file search.php:
<?php get_header(); ?>
<main class="container">
<?php if ( have_posts() ): ?>
<header>
<h1><?php printf( __( 'Pencarian untuk: %s', 'domain' ), '<span>' . esc_html( get_search_query() ) . '</span>' ); ?></h1>
</header>
<?php while( have_posts() ): the_post(); ?>
<div class="content">
<?php the_title( '<h2 class="title">', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>