2012-06-19 56 views
0

在主页我打印职位,我想在foreach循环中打印类别名称?我怎样才能做到这一点?如何获得职位类别

示例代码:

 if (have_posts()) : 

     $args = array(
      'showposts' => '5', 
      'paged' => $paged 
     ); 


     $thePosts = query_posts($args); 

     foreach($thePosts as $post) : setup_postdata($post); 

    ... 

回答

1

解决:

<?php 

      GLOBAL $wpdb; 

      $query = " 
      SELECT 
       terms.slug, r.term_taxonomy_id, terms.name AS cat_name 
      FROM 

       {$wpdb->prefix}term_taxonomy t, {$wpdb->prefix}terms terms, $wpdb->posts p, $wpdb->term_relationships r 

      WHERE 

       t.term_id=terms.term_id AND 
       p.ID = r.object_id AND 
       r.term_taxonomy_id = t.term_taxonomy_id AND 
       p.ID = '".$post->ID."' 

      ORDER BY terms.term_order ASC, terms.term_id DESC"; 

      $categories = $wpdb->get_results($query, ARRAY_A); 


      if(is_array($categories)){ 

       $i=1; 
       foreach($categories as $category) { 

        echo '<span>'.$category['cat_name'].'</span>'; 

        ++$i; 
       }#end foreach($categories 

      }#end if 


      ?> 
0

将此代码放在你的循环。

<?php 
/* translators: used between list items, there is a space after the comma */ 
$categories_list = get_the_category_list(__(', ', 'twentyeleven')); 

if ('' != $categories_list) { 
$utility_text = __('This entry was posted in %1$s', 'twentyeleven'); 
} 
printf($utility_text); 
?> 

这将打印当前帖子的类别。希望你使用二十几岁的孩子主题。

+0

您的解决方案是行不通的。没有什么印刷。 – CroiOS