2017-11-12 79 views
1

私はワードプレスコードでヘルプが必要です。 このページのhttps://developer.wordpress.org/reference/functions/get_the_terms/私の分類とその用語を返すコードがあります。特定のタクソノミを機能から除外します

しかし、このリストから除外する「広告タイプ」という特定のタクソノミーが欲しいです。

私は

// An array of IDs to ignore/exclude 
$ excluded_ids = array (1, 2, 3, 4); 

foreach ($ terms as $ term) { 
// Only proceed if the term_id is NOT in the $ excluded_ids array 
    if (! in_array ($ term-> term_id, $ excluded_ids)) { 
    $ out. = '<li> <a href="' .get_term_link($term-> slug, $ taxonomy).' '>'. $ term-> name. '</a> </ li>'; 
} 
} 

...このコードを使用して「広告タイプ」からいくつかの用語を除外するために管理しかし、私がしたいことは、「広告タイプ」分類がまったく表示されないということです。 上記のコードでは、検索された用語リストに「広告タイプ」のタイトルが表示されるためです。 私が望むのは、私のワードプレステンプレートのその部分で取り出されることからこのタクソノミーを完全に排除することです。ここで

はコードです:

function wpdocs_custom_taxonomies_terms_links() { 
// Get post by post ID. 
$post = get_post($post->ID); 

// Get post type by post. 
$post_type = $post->post_type; 

// Get post type taxonomies. 
$taxonomies = get_object_taxonomies($post_type, 'objects'); 

$out = array(); 

foreach ($taxonomies as $taxonomy_slug => $taxonomy){ 

    // Get the terms related to post. 
    $terms = get_the_terms($post->ID, $taxonomy_slug); 

    if (! empty($terms)) { 

     $out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>"; 
     foreach ($terms as $term) { 
      $out[] = sprintf('<li>%2$s</li> - ', 
       esc_url(get_term_link($term->slug, $taxonomy_slug)), 
       esc_html($term->name) 
      ); 
     } 
     $out[] = "</ul>\n"; 
    } 
} 

return implode('', $out); 
} 

そして、ここで私は私のテンプレートページ上でそれを取得する方法である:

<?php echo wpdocs_custom_taxonomies_terms_links(); ?> 

これを行う方法はありますか?誰でも助けてくれますか?

おかげ

+0

を取得する質問は、少し混乱してのみ、その後Ad Typeでない場合は、確認することができます「タクソノミが返されないように完全に除外」し、2つの別々のコードブロックがあるとします。したがって、関数 'get_the_terms()'は、フィルタを使用する場合はterm型の_except_を除外できません(ただし、get_the_termsが呼び出されるたびに実行されます)。あなたの機能の中で「排除」が起こるのは受け入れられますか? –

答えて

0

私はそれが正しいと比較していた場合、私は知りませんが、現在の分類は、あなたが言うので、用語

foreach ($taxonomies as $taxonomy_slug => $taxonomy){ 

    if ($taxonomy_slug !== 'ad_type') : 
     // Get the terms related to post. 
     $terms = get_the_terms($post->ID, $taxonomy_slug); 

     if (! empty($terms)) { 

      $out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>"; 
      foreach ($terms as $term) { 
       $out[] = sprintf('<li>%2$s</li> - ', 
        esc_url(get_term_link($term->slug, $taxonomy_slug)), 
        esc_html($term->name) 
       ); 
      } 
      $out[] = "</ul>\n"; 
     } 
    endif; 
} 
関連する問題