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 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?