web-dev-qa-db-fra.com

Comment exclure/filtrer une balise de get_the_tag_list ()

Est-ce que quelqu'un sait comment exclure/filtrer une balise de la chaîne HTML générée par get_the_tag_list ()?

http://codex.wordpress.org/Function_Reference/get_the_tag_list

Toute aide très appréciée.

2
Ben Pearson
function mytheme_filter_tags( $term_links ) {
    $result = array();
    $exclude_tags = array( 'some tag', 'another tag', 'third tag' );
    foreach ( $term_links as $link ) {
        foreach ( $exclude_tags as $tag ) {
            if ( stripos( $link, $tag ) !== false ) continue 2;
        }
        $result[] = $link;
    }
    return $result;
}
add_filter( "term_links-post_tag", 'mytheme_filter_tags', 100, 1 );

// do loop stuff
echo get_the_tag_list('<p>Tags: ',', ','</p>');
// end loop stuff

remove_filter( "term_links-post_tag", 'mytheme_filter_tags', 100 );
0
Eugene Manuilov

Ceci est un extrait de l'un de mes blogs:

function get_filtered_tags($post_id) {
    // the slugs to be INCLUDED in the term list:
    $primary_tags = array( 'books', 'travel', ... ); 

    $post_tags = wp_get_object_terms($post_id, 'post_tag');

    if( ! empty( $post_tags ) ) {
        if( ! is_wp_error( $post_tags )) {
            $tag_end = __( ', ', 'twentytwelve' );
            foreach ( $post_tags as $term ) {
                $term_slug = $term->slug;
                if ( in_array( $term_slug, $primary_tags) ) {
                    if ( isset( $tag_list ) ) {
                        $tag_list .= $tag_end;
                    }
                    $tag_list .= '<a href="' . get_term_link($term_slug, 'post_tag') . 
                        '">' . $term->name . '</a>'; 
                }
            }
        }
    }

    return $tag_list;
}

Si vous souhaitez exclure $primary_tags, procédez comme suit:

if ( ! in_array( $term_slug, $primary_tags) ) {
    ... 
}

C'est à dire. ajoutez un ! avant in_array( ... ).

0
Erk