Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using custom post type. I want to show every post's the most child category on archive page in wp_query loop. But the_category(''); doesn't work. What should I do? I need your help. Sorry for my bad English. Thanks in advance.

My codes:

archive-fotograf.php

<?php 
/**
 * Displays the Pagination in Custom loop
 *
 */
?>
<?php get_header(); ?>
<?php $aranan= $_GET['s']; 
$kategorim = intval($_GET['cat']);
$myterm = get_term( $kategorim, 'fgaleri' ); 
$myslug = $myterm->slug;
?>
<div id="content">
<?php
$temp = $wp_query; //save old query
$wp_query= null; //clear $wp_query
$rakam= 0;
//The query
$wp_query = new WP_Query();
$wp_query->query( array( 'post_type' =>'fotograf', 's' => $aranan, 'paged' => $paged, 'taxonomy' => 'fgaleri', 'fgaleri' => $myslug ) );
//The loop
?>
<div id="FGaleri_2"><div id="FGaleri_Ic"><div id="FGaleriBaslik">Fotoğraf Galerisi 
<form action="<?php echo get_bloginfo('wpurl'); ?>" id="searchformfoto" method="get" name="searchform">
<?php 
$args = array( 
'taxonomy' => 'fgaleri',
'show_option_none' => false,
'hierarchical' => 1
); 
wp_dropdown_categories($args); 
?>
<div id="aramabolumufoto"><input name="s" id="sfoto" type="text" value=""/>
<input type="hidden" name="post_type" value="fotograf" /> 
<input type="hidden" name="fgaleri" value="<?php echo $myslug; ?>" /> 
<input id="searchsubmitfoto" type="submit" value="Ara"/></div>
</form>
</div><div class="FGaleri_Ic_2">
<?php while ($wp_query->have_posts()): $wp_query->the_post(); 
$rakam++;
?>
<div class="FGaleriItem2" style="<?php if ($rakam % 5 == 0 ) { ?>margin-right: 0px;<?php } ?>"> 
<?php // the_category(''); IS NOT WORKING. I WANT TO POST CATEGORY COME HERE!
?>
<div class="ResimUst"><?php if (has_post_thumbnail()) { ?><a href="<?php the_permalink() ?>"><?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );$url2 = get_bloginfo('template_directory') . "/timthumb.php?src=" . $url . "&amp;w=100&amp;h=100&amp;zc=1"; ?><img src="<?php if(get_option('of_timthumb')=="true") { echo $url2; } else { echo $url; } ?>" alt="" title="" width="100" height="100" /></a><?php } ?></div><div class="ResimAlt"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php 
?>
</div></div><?php endwhile; ?></div></div></div><div class="clearboth"></div>
<?php if(function_exists('wp_pagenavi') ) {
 wp_pagenavi(); //function call for plugin pagination( wp pagenavi plugin)
}else{ ?>
 <div id="navigasyon">
      <span class="previous-entries"><?php next_posts_link('« Daha Eski Fotoğraflar'); ?></span>
  <span class="next-entries"><?php previous_posts_link('Daha Yeni Fotoğraflar »'); ?></span>
 </div>
<?php
}//endif
?>
<?php
wp_reset_postdata();
$wp_query = null; //Reset the normal query
$wp_query = $temp;//Restore the query

?>

</div><?php get_sidebar(); ?></div><?php get_footer(); ?>

functions.php

<?php
add_action( 'init', 'register_cpt_fotograf' ); 
function register_cpt_fotograf() { 
$labels = array( 
'name' => _x( 'Fotoğraflar', 'fotograf' ), 
'singular_name' => _x( 'Fotoğraf', 'fotograf' ), 
'add_new' => _x( 'Yeni Ekle', 'fotograf' ), 
'add_new_item' => _x( 'Yeni Fotoğraf Ekle', 'fotograf' ), 
'edit_item' => _x( 'Fotoğrafı Düzenle', 'fotograf' ), 
'new_item' => _x( 'Yeni Fotoğraf', 'fotograf' ), 
'view_item' => _x( 'Fotoğrafı Önizle', 'fotograf' ), 
'search_items' => _x( 'Fotoğraf Ara', 'fotograf' ), 
'not_found' => _x( 'Fotoğraf Bulunamadı', 'fotograf' ), 
'not_found_in_trash' => _x( 'Çöpte Fotoğraf Bulunamadı', 'fotograf' ), 
'parent_item_colon' => _x( 'Ana Fotoğraf:', 'fotograf' ), 
'menu_name' => _x( 'Fotoğraflar', 'fotograf' ), 
); 
$args = array( 
'labels' => $labels, 
'hierarchical' => false, 
'supports' => array( 'title', 'author', 'thumbnail', 'custom-fields', 'comments' ),
'public' => true, 
'show_ui' => true, 
'show_in_menu' => true, 
'show_in_nav_menus' => false, 
'publicly_queryable' => true, 
'exclude_from_search' => false, 
'has_archive' => true, 
'query_var' => true, 
'can_export' => true, 
'rewrite' => array('slug' => 'fotograflar'),
'capability_type' => 'post' 
); 
register_post_type( 'fotograf', $args ); 
} 

register_taxonomy("fgaleri", "fotograf", array("hierarchical" => true, "label" => "Fotoğraf Kategorileri", "singular_label" => "Fotoğraf Kategorisi", 'show_ui' => true, 'show_tagcloud' => false, "rewrite" => array('slug'=>'fkat')));

function mySearchFilter_0987($query) { 
$post_type = $_GET['post_type']; 
if (!$post_type) { 
    $post_type = 'post'; 
} 
$query->set('post_type', $post_type);
return $query; 
}; 

add_filter('pre_get_posts','mySearchFilter_0987');

// This filter will jump into the loop and arrange our results before they're returned

function limit_posts_per_archive_page() {   
if (is_post_type_archive()) { $limit = 20; } else { $limit = get_option('posts_per_page'); }        
set_query_var('posts_per_archive_page', $limit);
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');

if (!term_exists( 'Fotoğraf Galerisi', 'fgaleri') ) { 
wp_insert_term( 'Fotoğraf Galerisi', 'fgaleri',
array( 'description' => 'Fotoğraf Galerisi', 'slug' => 'fotogaleri' ) ); 
}

add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
    $query_vars['s'] = " ";
}
return $query_vars;
}
?>
share|improve this question
1  
hello, i've edited and improved code formatting for better readabilities. –  HungryCoder Jul 19 '12 at 14:18
    
I'm waiting for a good reply. Please help me! –  onur Jul 19 '12 at 15:26

1 Answer 1

up vote 0 down vote accepted

the_category() only works for the "Category" taxonomy which doesn't appear to be hooked to your custom post type. I see you're registering a custom taxonomy. If you want to show the terms from that taxonomy that are assigned to the post, you'll need to use the_terms() or get_the_terms():

the_terms( $post->ID, 'fgaleri', '', ', ', '' );
share|improve this answer
    
Omg! This is best answer I've read. :) Many thanks to you, mate. –  onur Jul 19 '12 at 16:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.