dqfaom241360 2014-01-09 18:34
浏览 45
已采纳

从自定义帖子类型中按ID获取类别名称

I'm trying to figure out how to fetch a category name/title via it's ID when it's part of a custom post type.

In this instance, I'm using Comic Easel, and for the custom post type of "comic" you have "chapters" and basically each chapter you add has an ID. In my case, chapter-1 (which is the slug) has an ID of 7.

I found out you can't call on a category by ID for a custom post type in the regular way you do it for categories created for WP posts or pages (which makes sense).

It's for a comic archive page, where the functionality is thumbnails of the comic pages are generated as they are posted and organized by the chapters they're categorized under.

Here's the code I'm using to generate thumbnails, page titles, and post dates for those pages in a specific chapter:

<div class="pagerow" id="Chapter 1">
<?php 
  $comicpage_loop = new WP_Query( array( 'chapters' => 'chapter-1', 'post_type' => 'comic', 'posts_per_page' => '999', get_post_meta($post->ID, 'ep_comic_posts_per_page', true), 'order' => 'ASC')); 
?>

<h2 class="chaptitle"></h2>
 <?php 
  while ($comicpage_loop->have_posts()) : $comicpage_loop->the_post();
 ?>

<div class="pages">
 <div class="pagetitle"><?php the_title(); ?></div>
    <div class="pagethumb">
    <a href="<?php the_permalink(); ?>">
     <?php 
       the_post_thumbnail('custom-2'); 
     ?>
    </a>
    </div>
  <div class="pubdate"><?php the_time('F j, Y') ?></div>
 </div>

  <?php endwhile; ?>
</div>

See the h2 tag "chaptitle"? That's where I'd like the chapter ie "Chapter 1" to display.

Is there a way I can pull up and display the chapter name in the query I already have going?

EDIT

Ok going by Boris's suggestion, I also found an example for get_term_child (I'm not using the child part but the rest of the syntax seemed helpful. Here's what I've ended up with and it does display the chapter title:

<?php
$term_id = 7;
$taxonomy_name = 'chapters';
$chapname = get_term($term_id, $taxonomy);
?>

<h2 class="chaptitle"><?php echo $chapname->name; ?></h2>

Is this right?

EDIT 2

I'm also finding adding this to my main query works as an alternative method:

$chapname = get_term($term_id = 7, $taxonomy_name ='chapters'),

I wonder though, is there a way to get the category ID in this query so I wouldn't have to put "7" for $term_id? To me not having to hardcode an id in would be ideal.

  • 写回答

1条回答 默认 最新

  • dongshuo9350 2014-01-09 18:41
    关注

    You can use get_term, where the second argument is taxonomy

    http://codex.wordpress.org/Function_Reference/get_term

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料