以自定义帖子类型显示父分类法 [英] Show parent taxonomy in custom post type

查看:67
本文介绍了以自定义帖子类型显示父分类法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组自定义帖子类型的学校,其位置按如下顺序排列:

I have a set of schools in a custom post type with locations ordered as follows:

London
- 1 Oxford Road
- 2 Cambridge Road

Paris
- 1 Napoleon Road
- 2 Tower Road

如何更改以下内容,以便输出位置父级而不是位置子级:

How do I change the following so that the location parent is outputted instead of the location child:

// begin loop
$args = array('post_type' => 'school');
query_posts($args); if ( have_posts() ) : while ( have_posts() ) : the_post();

// variable for location
$location = get_the_term_list( $post->ID, 'location', '', ', ', '' );

// output   
echo get_the_title() . ' - ' . $location;


// end loop
endwhile; endif;

谢谢。

推荐答案

我没有测试以下脚本,但我希望它可以带您进一步解决该问题。

I didn't test the following script, but I hope it will bring you a step forward to the solution.

// begin loop
$args = array('post_type' => 'school');
query_posts($args); if ( have_posts() ) : while ( have_posts() ) : the_post();

// variable for location
$term_list = '';
$terms     = get_the_terms( $post->ID, 'location' );
$prefix    = '';

foreach( $terms as $term ) {
    $parent_term = get_term( $term->parent, 'location' );
    $term_list  .= $prefix . $parent_term->name . ' - ' . $term->name;
    $prefix      = ', ';
}

// output
echo get_the_title() . ' - ' . $term_list;

// end loop
endwhile; endif;

这篇关于以自定义帖子类型显示父分类法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆