Cara membuat looping post/artikel berdasarkan kategori tertentu sangat mudah,
Pada artikel sebelumnya untuk membuat looping post kode yang kita ketikan adalah sebagai berikut :
<?php query_posts("showposts=6") ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><br />
<?php echo get_the_category_list(', '); ?>
<?php the_time('d/m/Y g:i:s A'); ?><br />
<img src="<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large'); echo $img[0]; ?>"/><br />
<?php the_excerpt(); ?> <br />
<?php endwhile; ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><br />
<?php echo get_the_category_list(', '); ?>
<?php the_time('d/m/Y g:i:s A'); ?><br />
<img src="<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large'); echo $img[0]; ?>"/><br />
<?php the_excerpt(); ?> <br />
<?php endwhile; ?>
<?php endif; ?>
Untuk menampilkan postingan dari kategori tertentu kita cukup mengubah pada bagian query_posts nya.
jika sebelumnya kode query_post untuk looping sebanyak 6 postingan adalah seperti berikut
<?php query_posts("showposts=6") ?>
misal id kategori yang akan kita tampilkan adalah 5 (full looping) maka kode menjadi
<?php query_posts("cat=5") ?>
Apabila Anda ingin menampilkan 6 post yang akan di looping dengan id kategori 5 maka kodenya untuk query_post adalah
<?php query_posts("showposts=6" . "&cat=5") ?>
~~hdra~~
14 comments
commentssaya ingin menampilkan 9 postingan dari 3 categori di homepage, cohtoh gini gan :
Reply3 postingan dari kategori KULINER
3 postingan dari kategori TEKNOLOGI
3 postingan dari kategori WEBDESIGN
itu caranya gimana ya gan..???
misal kuliner id kategorinya 1, teknologi idnya 2 , webdesign idnya 3 ,
Replyjadi dibikin 3 kali looping dengan masing query postnya beda
looping pertama kuliner query postnya
query_posts('showposts=3&cat=1');
looping teknologi
query_posts('showposts=3&cat=2');
looping webdesign
query_posts('showposts=3&cat=3');
masih kesulitan belajar wordpress, tipsnya gimana ya mas?
Replybeli bukunya biasanya lebih lengkap, terus di praktekin..
Replygan masih bingung...
Replytolong ganti gan, di temaku kayak gini (klik link)
https://plus.google.com/115565955062443169529/posts/KmLQdMR58sT
Reply<!-- munculkan 3 postingan yang termasuk dlm category1 -->
<?php query_posts("showposts=3&cat=1") ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $c = get_the_category(); ?>
<a href='<?php the_permalink() ?>' title='<?php the_title(); ?>' ><img src='<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); echo $img[0]; ?>'/> <?php echo $c[0]->cat_name; ?> <?php the_title(); ?> </a>
<?php endwhile; ?>
<?php endif; ?>
<!-- munculkan 3 postingan yang termasuk dlm category 2 -->
<?php query_posts("showposts=3&cat=2") ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $c = get_the_category(); ?>
<a href='<?php the_permalink() ?>' title='<?php the_title(); ?>' ><img src='<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); echo $img[0]; ?>'/> <?php echo $c[0]->cat_name; ?> <?php the_title(); ?> </a>
<?php endwhile; ?>
<?php endif; ?>
<!-- munculkan 3 postingan yang termasuk dlm category 3 -->
<?php query_posts("showposts=3&cat=3") ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $c = get_the_category(); ?>
<a href='<?php the_permalink() ?>' title='<?php the_title(); ?>' ><img src='<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); echo $img[0]; ?>'/> <?php echo $c[0]->cat_name; ?> <?php the_title(); ?> </a>
<?php endwhile; ?>
<?php endif; ?>
-- keterangan --
hasil dari script diatas akan memunculkan 3 postingan yang termasuk dalam kategori 1, 3 postingan yang termasuk dalam kategori 2, dan 3 postingan yang termasuk dalam kategori 2 (kita harus tau dulu id kategorii yang akan kita munculkan, kalo kategori teknologi id kategorinya 6 ya kita masukan angka 6 sebagai id kategorinya). looping diatas tidak memunculkan nomer halaman, karna saya memang tidak menggunakan fungsi untuk memunculkan nomer halaman,
<?php the_permalink() ?> fungsinya untuk munculkan link post,
<?php the_title(); ?> fungsinya memunculkan judulpost
<img src='<?php $img=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); echo $img[0]; ?>'/> fungsinya memunculkan gambar post dalam ukuran medium
<?php echo $c[0]->cat_name; ?> fungsinya memunculkan nama kategori post
Oke gan terimaksih banyak, ane coba dulu :)
Replygan mau nanya, cara bikin homepage 1 post tertentu secara penuh di "wp" gimana caranya ?
Replymohon penjelasannya.
trims
gan mau nanya, cara bikin homepage 1 post tertentu secara penuh di "wp" gimana caranya ?
Replymohon penjelasannya.
trims
Sangat rumit juga ya buatnya
Replyitu di paste dimana gan?
ReplyTerimakasih atas infonya dan keterangannya gan,
ReplyArtikelnya sangat membantu saya...
di tunggu artikel selanjutnya gan.
susah juga yaa gan tolong dibantu untuk pembuatan web saya
Replyhttps://servicehandal.com
mas itu loppongnya masihkan dapat disematkan di HTML5?
ReplyKomentar disini ..