以自定义帖子类型显示父分类法

问题描述 投票:3回答:2

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

London
- 1 Oxford Road
- 2 Cambridge Road

Paris
- 1 Napoleon Road
- 2 Tower Road

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

// 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;

谢谢。

wordpress taxonomy custom-post-type
2个回答
5
投票

我没有测试以下脚本,但是希望它可以使您朝解决方案迈出一步。

// 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;

0
投票

将'work_categories'更改为您的分类名称-以下代码已通过测试,但不适用于5.3之类的旧PHP版本。

© www.soinside.com 2019 - 2024. All rights reserved.