【问题标题】:What can I use instead of get_the_category() for custom taxonomies我可以使用什么来代替 get_the_category() 进行自定义分类
【发布时间】:2012-12-20 04:37:13
【问题描述】:

基本上,我在 WordPress 中使用自定义分类法 (custom_tax) 创建了自定义帖子类型。现在我创建了一个自定义查询,我想在其中获取保存自定义帖子的自定义类别并将其名称作为字符串输出。我在使用 WordPress 标准分类时这样做:

//Start the query
   query_posts(array(
  'showposts' => $posts,
  'orderby' => 'asc',
  'category_name' => $category
));

$temp_title = get_the_title();
$temp_link = get_permalink();
$temp_excerpt = get_the_excerpt();
$temp_time = get_the_date('Y-m-d');
$temp_categories = get_the_category(', ');
$temp_author = get_the_author();
$temp_content = get_the_content();
$i++;

$output2 .= "<li class='item' data-id='id-" . $i . "' data-type='" . $temp_categories . "'><a href='" . $temp_link . "' rel='prettyPhoto[portfolio]'><img class='' src='" . $path . "/library/timthumb.php?src=" . $image[0] . "&h=130&w=210&zc=1&q=100' alt='" . $temp_title . "' /></a></li>";

它就像一个魅力。现在我需要使用自定义帖子和自定义分类,并尝试了很多变体:

//Start the query
query_posts(array(
  'post_type' => array('post', 'portfolio'),
  'showposts' => $posts,
  'orderby' => 'asc',
  'category_name' => $category
));

$temp_title = get_the_title();
$temp_link = get_permalink();
$temp_excerpt = get_the_excerpt();
$temp_time = get_the_date('Y-m-d');
$temp_categories = get_the_terms( get_the_ID(), 'custom_cat', ', ' );
$temp_author = get_the_author();
$temp_content = get_the_content();
$i++;

$output2 .= "<li class='item' data-id='id-" . $i . "' data-type='" . $temp_categories . "'><a href='" . $temp_link . "' rel='prettyPhoto[portfolio]'><img class='' src='" . $path . "/library/timthumb.php?src=" . $image[0] . "&h=130&w=210&zc=1&q=100' alt='" . $temp_title . "' /></a></li>";

而且它不起作用。我已经尝试过 get_the_terms()、the_terms() 和 get_terms(),但没有一个像 get_the_category() 之前那样有效——它输出的是一个带有类别名称的字符串。我错过了什么?

非常感谢

【问题讨论】:

    标签: wordpress custom-post-type custom-taxonomy


    【解决方案1】:

    Wordpress 将分类法描述为“术语”。他们的“get_terms()”文档中可能有一些帮助,找到了here。如果您想按术语排序,有一个 great post on the wordpress forums 可以做到这一点!

    【讨论】:

    • 谢谢马修。我之前经历过 get_terms() 并且输出与我需要的不同。我只需要保存此特定帖子的类别即可显示在帖子的其他位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多