简体   繁体   中英

How exclude one term from get_terms

I am showing a list of terms from a taxonomy and need to exclude a term from the list how do I do this? I made the code below but did not delete it.

$terms = get_terms( 'taxonomyespecialidades', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => array(),
) );
$exclude = array("Acupuntura");
$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);

You can try like this:

You can exclude multiple terms from get_terms by putting their id and slugs in it. for example:

// default to not exclude terms
$ids_to_exclude = array();
$get_terms_to_exclude =  get_terms(
    array(
        'fields'  => 'ids',
        'slug'    => array( 
            'Acupuntura', 
            'Acupuntura2', 
            'Acupuntura3',
            'Acupuntura4' ),
        //'taxonomy' => 'put taxonomy name here',
    )
);

if( !is_wp_error( $get_terms_to_exclude ) && count($get_terms_to_exclude) > 0){
    $ids_to_exclude = $get_terms_to_exclude; 
}
$terms = get_terms( 'taxonomyespecialidades', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => $ids_to_exclude,
) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM