3

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

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;

谢谢你。

4

2 回答 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;
于 2013-03-12T15:20:32.707 回答
0

将“work_categories”更改为您的分类名称 - 下面的代码已经过测试,但不适用于较旧的 PHP 版本,例如 5.3。

// @codingStandardsIgnoreStart
$first_term_name = get_the_terms( $post->ID, 'work_categories' )[0]->name;
var_dump( $first_term_name );
// @codingStandardsIgnoreEnd
于 2020-05-19T10:23:54.577 回答