1

我的代码如下。使用条款删除是行不通的。我需要它像这样工作而不是按 ID 删除。

$terms = get_terms( 'MY_TAXONOMY', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => array(),
) );
$exclude = array("MY TERM", "MY TERM 2", "MY TERM 3");
$new_the_category = '';
foreach ( $terms as $term ) {
    if (!in_array($term->term_name, $exclude)) {
        $new_the_category .= '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
    }
}
echo substr($new_the_category, 0);
4

2 回答 2

1

您可以通过使用您想要省略的条款来获得term_ids您想要排除的那些。get_term_by()然后您可以将这些 id 作为排除参数传递。

请注意,第二个$args数组 inget_terms()已被弃用,因此您应该MY_TAXONOMY使用 key 进入参数taxonomy

另外,我不确定您为什么要回显从 0 开始而没有端点的子字符串,因此我将其删除。我还删除了变量连接,只是在 foreach 循环中回显了字符串。

$exclude_ids   = array();
$exclude_names = array("MY TERM", "MY TERM 2", "MY TERM 3"); // Term NAMES to exclude

foreach( $exclude_names as $name ){
    $excluded_term = get_term_by( 'name', $name, 'MY_TAXONOMY' );
    $exclude_ids[] = (int) $excluded_term->term_id; // Get term_id (as a string), typcast to an INT
} 

$term_args = array(
    'taxonomy' => 'MY_TAXONOMY',
    'orderby' => 'name',
    'order'   => 'ASC',
    'exclude' => $exclude_ids
);

if( $terms = get_terms( $term_args ) ){
    // If we have terms, echo each one with our markup.
    foreach( $terms as $term ){
        echo '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
    }
}
于 2018-06-23T02:06:18.490 回答
1

您的代码运行良好,只需将$term->term_name替换为$term- >name即可。请参阅下面的代码以供参考。

$terms = get_terms( 'MY_TAXONOMY', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => array(),
) );
$exclude = array("MY TERM", "MY TERM 2", "MY TERM 3");
$new_the_category = '';
foreach ( $terms as $term ) {
if (!in_array($term->name, $exclude)) {
$new_the_category .= '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
}
}
echo substr($new_the_category, 0);
于 2018-06-23T06:00:20.630 回答