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'を分類法名に変更します-以下のコードはテストされていますが、5.3などの古いバージョンのPHPでは機能しません。

// @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 に答える