2015-04-05 55 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); 
相关问题