2013-03-12 35 views
1

我有一組在自定義後類型學校訂購地點如下:顯示父分類

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

我沒有測試下面的腳本,但我希望它能帶給你一個解決方案。

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

Mike,這真是太棒了謝謝。奇蹟般有效! – user18577 2013-03-12 15:34:27

+0

經過2天的搜索,我發現這...謝謝。我瘋了。 – gleenk 2013-08-09 09:53:24