2015-04-05 43 views
0

我想通過單詞獲取現有術語。WordPress通過單詞獲取分類術語

例如:將顯示包含單詞「good」的所有關於「產品」的術語。

所以我們得到:好書,好電影,好玩具等

看看這段代碼:

<?php 
 
$args = array('hide_empty=0'); 
 

 
$terms = get_terms('products', $args); 
 
if (! empty($terms) && ! is_wp_error($terms)) { 
 
    $count = count($terms); 
 
    $i = 0; 
 
    $term_list = '<p class="my_term-archive">'; 
 
    foreach ($terms as $term) { 
 
     $i++; 
 
    \t $term_list .= '<a href="' . get_term_link($term) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>'; 
 
    \t if ($count != $i) { 
 
      $term_list .= ' &middot; '; 
 
     } 
 
     else { 
 
      $term_list .= '</p>'; 
 
     } 
 
    } 
 
    echo $term_list; 
 
} 
 
?>
我把它從 https://codex.wordpress.org/Function_Reference/get_terms

此代碼告訴我所有來自「產品」的術語,但我只想從「產品」中包含「良好」一詞的術語。

回答

0

一旦你有了完整的分類術語數組,你可以使用PHP的preg_grep()函數過濾掉包含單詞「good」的術語。

$terms = get_terms('products', $args); 

// This will return all terms that contain "good" in some form, as in 'good books' or 'goodness' 
$goodterms = preg_grep("/good/", $array); 
相關問題