2013-07-22 145 views
0

我真的需要你的幫助。我剛剛創建了一個顯示所有帖子的WordPress頁面模板,但是我的問題是顯示了自定義父分類法/類別,並且它是孩子。我的帖子看起來像這樣。自定義父類別和子類別

<table> 
    <tr> 
    <td>Title</td> 
    <td>Parent Category</td> 
    <td>Sub Category</td> 
    <td>Excerpt</td> 
    </tr> 
    <tr> 
    <td>a title</td> 
    <td>USA custom category</td> 
    <td>Hawaii uder USA</td> 
    <td>this is a sample description.</td> 
    </tr> 
    </table> 

我唯一的問題是顯示子類別。任何人都可以幫助我?

這是我的我是如何顯示父自定義類別上代碼:

<?php 

    $term_list = ''; 
    $terms  = get_the_terms($post->ID, 'origincity'); 
    foreach($terms as $term) { 
     $parent_term = get_term($term->parent, 'origincity'); 
     $term_list .= $parent_term->name ; 
    } 
    echo $term_list; 
    ?> 

我試圖通過這個代碼,以顯示子類別:

<?php $terms2 = wp_get_post_terms($post->ID, 'origincity', array("fields" => "all")); 

foreach ($terms2 as $term1) { 
    echo $term1->name.'<br>'; 
} ?> 

,但它也返回父。 :(

你的幫助是非常讚賞。謝謝你。

+0

你想只列出帖子?或只是標題? – designtocode

+0

是列出帖子@msbodetti – rolex

回答

0

通過這段代碼已經解決的問題:順便說

function print_taxonomic_ranks($terms){ 


    // set id variables to 0 for easy check 
    $order_id = $family_id = $subfamily_id = 0; 


    // get family 
    foreach ($terms as $term) { 
     if ($family_id || $order_id != $term->parent) 
      continue; 
     $family_id = $term->term_id; 
     $family = $term->name; 
    } 

    // get subfamily 
    foreach ($terms as $term) { 
     if ($subfamily_id || $family_id != $term->parent) 
      continue; 
     $subfamily_id = $term->term_id; 
     $subfamily = $term->name; 
    } 

    // output 
    echo "$subfamily"; 

} 

感謝。

0

我不建議使用你目前正在使用的方法,因爲你沒有過什麼出足夠的控制權。

在將來使用get_posts();

這真的很簡單易用!例如:

<ul> 
<?php 

global $post; 

$args = array('posts_per_page' => 5, 'offset'=> 1, 'category' => 1); 

$myposts = get_posts($args); 

foreach($myposts as $post) : setup_postdata($post); ?> 
    <li> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
<?php endforeach; ?> 

</ul> 

正如你可以看到你可以設置多少職位,在範疇上y以及更多顯示帖子的參數。

詳細瞭解這裏的論點http://codex.wordpress.org/Template_Tags/get_posts