2015-06-15 77 views
1

我需要呼应职位类别链接,所以这里是我的查询:获取类别链接WordPress的

<?php 
    $args=array(
      'cat' => 3, 
      'post_type' => 'post', 
      'post_status' => 'publish', 
      'posts_per_page' => 3, 
      'caller_get_posts'=> 1 
     ); 
     $my_query = null; 
     $my_query = new WP_Query($args); 
     if($my_query->have_posts()) { 
      while ($my_query->have_posts()) : $my_query->the_post(); ?> 

       ....SOME HTML.... 

     <?php 
       endwhile; 
     } 
     wp_reset_query(); 
    ?> 

和底部,我需要点击“查看所有文章”,它应该重定向到后子类别列表。

+1

获取帖子的ID并使用函数get_the_category(post_id)获取类别,帖子可能属于多个类别。从get_the_category()返回的对象中检索类别id,现在使用函数get_category_link(cat_id)获取类别链接 – WisdmLabs

回答

0

只需使用Wordpress的功能the_category。第二个参数single确保只显示子类别。您可以在documentation中找到关于它的更多详细信息。
如果你在while循环中使用下面的代码,它应该可以工作。

<?php 
$postID = get_the_ID(); 

the_category(', ', 'single', $postID); 
?> 

我希望这会有所帮助。