WordPress通过单词获得分类术语

时间:2015-04-05 21:52:46

标签: 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++;
    	$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
    	if ( $count != $i ) {
            $term_list .= ' &middot; ';
        }
        else {
            $term_list .= '</p>';
        }
    }
    echo $term_list;
}
?>
&#13;
&#13;
&#13; 我是从https://codex.wordpress.org/Function_Reference/get_terms

取的

此代码显示了&#34;产品&#34;中的所有条款,但我只想要来自&#34;产品&#34;的条款包含&#34; good&#34;。

1 个答案:

答案 0 :(得分:0)

获得完整的分类术语后,您可以过滤掉包含“&#34; good&#34;使用PHP的preg_grep()函数。

$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);
相关问题