Menampilkan Custom Post Type Berdasarkan Custom Taxonomies

Selama 2 minggu ini saya sedang mencari jawaban untuk kostumasi themes wordpress untuk Website Karisma Academy yang baru…

Saya memiliki custom post type “paket_kursus” dan custom taxonomiesnya “kursus_cat” (kategori kursus).

Masalah:

Bagaimanakah menampikan seluruh posting (paket kursus) berdasarkan kategori/taxonomy “kursus_cat” yang ada?

Custom post type dan Custom Taxonomy tersebut didaftarkan melalui functions.php sebagai berikut:

Mendaftarkan Custom Post Type


		$labels = array(
			'name' => __( 'Kursus', '' ),
			'singular_name' => __( 'Kursus', '' ),		
			'add_new' => _x( 'Add New', 'Kursus', '' ),
			'add_new_item' => __( 'Add New Kursus', '' ),
			'edit_item' => __( 'Edit Kursus', '' ),
			'new_item' => __( 'New Kursus', '' ),
			'view_item' => __( 'View Kursus', '' ),
			'search_items' => __( 'Cari Kursus', '' ),
			'not_found' =>  __( 'No Kursus Projects found', '' ),
			'not_found_in_trash' => __( 'Tidak Menemukan Kursus di Tempat Sampah', '' ),
			'parent_item_colon' => '',
			'menu_name' => _x( 'Paket Kursus','Paket Kursus' ),
		);

		//build kursus type
		$args=array(
			'labels'=>$labels,
		  	'public' => true,
		  	'description' => 'Kursus',
		  	'supports' => array('title','editor','thumbnail','excerpt'),
		  	'query_var' => true,
        	'rewrite' => array('slug' => 'paket-kursus', 'with_front' => false),
 			'show_ui' => true,
		  	'show_in_menu' => true,
		  	'show_in_nav_menu' => true,
		  	'publicly_queryable' => true,
       		'exclude_from_search' => false,
        	'has_archive' => true,
		  	'can_export' => true,
		  	'menu_position' => 24,
		  	'capability_type' => 'post',
		  	'menu_icon' => get_template_directory_uri().'/images/icon/kursus.png',  // Icon Path
		);

	register_post_type('paket_kursus',$args);

Membuat Kategori Kursus (Custom Taxonomy)


$cat_kursus = array(
		'name' => __( 'Kursus Categories', '' ),
		'singular_name' => __( 'Kursus Category', '' ),
		'search_items' =>  __( 'Search Kursus Categories', '' ),
		'all_items' => __( 'All Kursus Categories', '' ),
		'parent_item' => __( 'Parent Kursus Category', '' ),
		'parent_item_colon' => __( 'Parent Kursus Category:', '' ),
		'edit_item' => __( 'Edit Kursus Category', '' ),
		'update_item' => __( 'Update Kursus Category', '' ),
		'add_new_item' => __( 'Add New Kursus Category', '' ),
		'new_item_name' => __( 'New Kursus Category Name', '' ),
		'choose_from_most_used'	=> __( 'Choose from the most used Kursus categories', '' )
	); 
register_taxonomy('kursus_cats','paket_kursus',array(
		'hierarchical' => true,
		'labels' => $cat_kursus,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'kursus-category' ),
	));

 

Solusinya:

<?php
$terms = get_terms('typeofproject');

foreach ($terms as $term) {
  $wpq = array ('taxonomy'=>'typeofproject','term'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count;
  echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
  echo $term->name;
  echo "</h3>";
  if ($article_count) {
    echo "<ul>";
    while ($myquery->have_posts()) : $myquery->the_post();
      echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
    endwhile;
    echo "</ul>";
  }
}
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s